CodeX
Published in

CodeX

Learning Python From Scratch: Data Structures —Dictionary

Hello everyone! I am one of Sisterslab’s Women in Tech Academy project participants, supported by Toplum Gönüllüleri Vakfı. The project aims to empower 25 women between the ages of 20–28 with software training for 3 months and seek their participation in the workforce in the sector. You can find more information about the project at this link: https://sisterslab.co/women-in-tech-academy/

I will try to cover python from scratch in this medium article series. In today’s article, I’m going to explain one of the data structures from Python, dictionaries. You can check the other data structure or python articles from my medium account!

Photo by Roman Synkevych 🇺🇦 on Unsplash

Dictionary

We’ve talked about lists, tuples, and sets in previous data structure chapters in my medium series. Now it's time to learn about dictionaries, another data structure in python. Compared to other data structures in python, dictionaries have a more complex structure. But we can think of this data structure as some kind of dictionary, as its name signifies. As all dictionaries contain words and their corresponding explanations, dictionaries in python consist of keys and their corresponding values. So in this data structure information is stored as key-value pairs. So let’s have a look at the other properties of dictionaries.

  • Dictionaries are unordered, so we are unable to access the index of items.
  • We can only access values via their unique keys.
  • Dictionaries can be changed so they are mutable. It allows insertion and subtraction operations. Also, we can change the value of keys.
  • We can define it with curly brackets {}. The key-value pairs are separated by commas.
  • May contain different data types.
  • Key values can be unique, but values can be duplicated.

Creating Dictionaries

There are two ways of creating a dictionary, these are:

  • {} -> Using colon(:) between key-value pairs and using comma(,) between every pair
  • dict()

When creating a Dictionary, we do not have to write all key-value pairs or all key values of the same type. That is, a dictionary can have both string and integer values at the same time, as in the code example below.

Or we can keep a list in the value part.

Dict()

Another definition is to create a dictionary using the dict() method. You can easily add the dictionary you want to define into the method as the example below.

Empty Dictionary

And if you want to define an empty dictionary, just define it with curly brackets!

Accessing Values in Dictionary

There are two ways to access values in dictionaries, those are:

  • [Key]
  • get(Key)

[Key]

If you want to access the values, just type the name of the dictionary and write the key value in the square brackets.

get(Key)

You can also access these values by using the get method, just after the dictionary put a dot and write the unique key you want to know the value of in the get method!

Accessing all keys or values in Dictionary

We learned how we can access certain values by using the key above, but if you want to access all key values in a dictionary at the same time you need to use their methods like the examples below.

Accessing Keys

For accessing all the keys in a dictionary, all you need to do is use the keys() method.

Accessing Values

And for the values in a dictionary, you can use the values() method.

Items in Dictionary

Or you can display all the items by using the items() method.

Accessing Items in Dictionary with Loops

You may also want to access keys, items, or values in a loop for the operations. It’s actually really simple to do this. We use the keys, values, or items method in a for loop to iterate through them like below.

Length of a Dictionary

To find the length of a dictionary, it is sufficient to write the dictionary name inside the len() function.

Adding Item to Dictionary

To add items to a dictionary, you should define the new item as dictionary_name[key] = value in code.

Changing Values

And if you want to change the value of a key, just define the new value as you're defining a new item like example below.

Updating Items

To update the dictionary, you need to define the values you want to add as a sub-dictionary inside the update() method.

Deleting Operations

There are many deleteing operations that we can use on the dictionary:

  • del key
  • clear()
  • del dict_name
  • pop(key)
  • popitem()

Del Key

This method takes a key value in the dictionary and deletes that item from the dictionary.

Clear()

The clear method deletes all elements in the entire dictionary. But doesn't delete the dictionary. Therefore, it does not give an error when you try to access the dictionary later.

Del dict_name

This method promptly deletes the entire dictionary. Unlike the clear method, it will give an error when you want to use the dictionary name afterwards.

pop(Key)

And pop(key) method takes a key value and removes the item corresponding to that key value from the dictionary.

popitem()

Removes the last item from the dictionary. And it makes an error when you try it with an empty list.

Merging Two Dictionaries

There is two ways to merge dictionaries:

  • update()
  • Using **

Update()

You can merge these two dictionaries by adding another dictionary name inside the update method. Thus, the dictionary you write in the method is added after the first dictionary.

Using **

You can also merge two or more dictionaries as in the example below.

Copying Dictionary

We can copy dictionaries in various ways:

  • = operator
  • copy()
  • dict()

= operator

If we use the equal(=) operator, the new dictionary will show the exact memory location of the old dictionary. Therefore, each change we make will affect the other dictionary.

copy()

If we want to create a new dictionary with a different location than the old dictionary in memory, we need to use the copy method.

dict()

We can also create a dictionary copy using the dict() method.

Sorting

The sorted() method is used to sort dictionaries. But in order to be able to sort, the entire dictionary must consist of the same type of key-value pairs.

For example the code below gives an error because it needs to be of the same type to sort.

And we’ve come to the end of another medium article! I hope you enjoyed it. You can also check out my other articles about Python from here.

Also, don’t forget to follow me and my other social media accounts.

You can find all the resources I used in my article here!

Sources:

Geeks For Geeks Python Data Structures

Educative Python From Scratch

Dictionaries in Python

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Elif Çelik

Elif Çelik

A Computer Engineer who is interested in Machine Learning and Deep Learning.