Dictionaries in Python

We have discussed sequences or collections in Python in the previous posts. They are basic data structures in Python that are used to group data together. 

We will discuss another type of sequences available in Python today called Dictionaries. A dictionary is a sequence of objects which is unordered but changeable and indexed.

Creating Dictionaries

Dicts are denoted by curly brackets in Python and consist of elements in pairs called key: value pairs. To create a dictionary, you can write the key: value pairs and store it in a variable.

For example, if you want to store information about a country in a dictionary, you can save all attributes with their specific names.

myDict = {“country”: “Pakistan”, “year”: 1947, “capital”: “Islamabad”}
print(myDict)

When the above code is executed, it produces the following result:

{country: “Pakistan”, year: 1947, capital: “Islamabad”}

Note:

  • In a dictionary, keys have to be unique while values may not be.
  • Keys have to be of immutable types like int, string, or tuple, while values can be of any data type.

Accessing Elements in Dictionaries

To access the values of a dictionary, we use the keys as indices. Considering the above dictionary, we can do:

myDict[‘country’]
myDict[‘capital’]

When the above code is executed, it produces the following result:

‘Pakistan’
‘Islamabad’

Also, you can loop through the dictionary and access values using keys just like you would using indices in lists.

for i in myDict:
  print(i)

When the above code is executed, it produces the following result:

‘Pakistan’
1947
‘Islamabad’

You can use the in keyword to check if a key is present in a dictionary or not.

print(“year” in myDict)

When the above code is executed, it produces the following result:

True

Modifying Dictionaries

To add a value to a dictionary, just write the key in brackets and assign a new value to it.

myDict[“provinces”] = 5
print(myDict)

When the above code is executed, it produces the following result:

{country: “Pakistan”, year: 1947, capital: “Islamabad”, provinces: 5}

Similarly, to update the value of a key, assign a new value to it.

myDict[“year”] = 1940
print(myDict)

When the above code is executed, it produces the following result:

{country: “Pakistan”, year: 1940, capital: “Islamabad”, provinces: 5}

Deleting Dictionaries and Dictionary Elements

Deleting dictionaries is simple like all other sequences, just use the del keyword with the name of the dictionary and it will be deleted.

del myDict

Using del with an indexed key will delete that particular entry in the dictionary.

del myDict[‘year’]
print(myDict)

When the above code is executed, it produces the following result:

{country: “Pakistan”, capital: “Islamabad”, provinces: 5}

You can also use clear with dictionaries to empty the dictionary without deleting its instance in the memory. 

myDict.clear()
print(myDict)

When the above code is executed, it produces the following result:

{}

This was all about creating and performing different operations on Dictionaries in Python. If you have any questions regarding the functionalities, do ask in the comments below.

Imad

I am a Software Engineer with ample experience in making games, websites, mobile apps and augmented reality solutions.

Pin It on Pinterest