Python Dictionary Data Type


Gaming Laptop Series

Python dictionary is an unordered collection of data values it stores data in key:value pairs. A pair of braces i.e. {} without any value creates an empty dictionary. You can initialize it by providing key and value pairs separated with a comma.

In this article, you will learn the Python dictionary and different operations that can be performed on it.

How to create Python dictionaries

A Python dictionary can be created in two ways first is that you place the comma-separated items inside curly braces and the second is to create using dict() function.

For example –

# Create an empty dictionary
user0={}
# Create dictionary with first method
user1={'name' : 'Vinay', 'age' : 22}
# Create dictionary using dict() function
user2= dict(name= 'Sumit', age=24)
# Now dispaly and check their data type
print(user0)
print(type(user0))
print(user1)
print(type(user1))
print(user2)
print(type(user2))

You can see the output in the given image –

Use the following syntax if the dictionary has too many items this will make code more readable –

user3={
'name':'Ankit',
'age': 25,
'place':'Lucknow',
'country':'India'
}

Accessing data from Python dictionary

A Python dictionary is an unordered collection of data so there is no indexing using which we can access the dictionary elements. Instead of using indexing, we use the key to access values corresponding to it.

You can use keys inside square brackets or get() method to access a value.

For example –

user1={'name' : 'Vinay', 'age' : 22}
print(user1[name])
# OR use get() method to access values
print(user.get(age))

Now see the output of this program in the given image –

Adding data to a dictionary

Dictionaries are mutable which means we can add, remove or update the elements of a Python dictionary. Let us have the following dictionary –

user3={
'name':'Ankit',
'age': 25,
'place':'Lucknow',
'country':'India'
}

To update the place from Lucknow to Delhi you can use –

user3['place'] = 'Delhi'

OR to add an item let’s say pin we will use the following statement –

user3['pin code']= 110001

Now display the update dictionary by using –

print(user3)

You can see the updated dictionary in the given image –

Removing Python dictionary elements

To remove an element from a dictionary you can use the pop() method. The pop() method takes the key as an argument.

user3={
'name':'Ankit',
'age': 25,
'place':'Lucknow',
'country':'India'
}

In the above dictionary to remove place, we will use the following statement –

print(user3.pop('place'))

Similarly, the popitem() method randomly deletes an element or key-value pair from the dictionary.

To remove all the items and print the dictionary use –

user3.clear()
print(user3)

You can delete a dictionary itself by using the del keyword-

del user3

Now see the output in the given image –

Looping in dictionary

There are different ways in which you can iterate over a dictionary.

  • Iterate through all keys
  • Iterate through all values
  • Iterate through all keys, value pairs

Lets we have the following dictionary –

user3={
'name':'Ankit',
'age': 25,
'place':'Lucknow',
'country':'India'
}

Now you can iterate and print all the keys of the user3 dictionary

for i in user3:
     print(i)

You can see the output in the given image –

To print all the values we will use the values() method as it is given below –

for i in user3.values():
        print (i)

If you run the program it will display all the values of the dictionary user3 –

By using the items() method you can print all key-value pairs of a dictionary.

for i in user3.items():
       print (i)

You can see the output in the given image-

Python dictionary methods

There are some methods that can be used with dictionaries in Python. We have already used some of them now see the complete list of these methods below.

Ok, that’s all for now. Now if you have a query please write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.