Dictionary Methods In Python

We’ve learned about the python dictionary and how they can store the values as a key:value pair in your variable. They are useful, and you need them in your python programs, but there is something called methods that extend their functionalities. You will explore most of them in this article

1. Clear() Method

The name of this method speaks for itself. It will be applied to the dictionary and make them empty of items, either the keys or the values. Let me show you an example:

# Create a Dictionary
Capitals = {"Germany":"Berlin", "UK":"London", "USA":"Washington"}

# Clear All Dictionary Items
Capitals.clear()

# Print The Dictionary
print(Capitals)

I’ve used Jupyter Notebook for this tutorial, and it will be better to use the same IDE while following this tutorial. When you execute the code on the Jupyter Notebook cell, it will give you empty curly brackets. That means you’ve cleared all the values and keys.

2. Get() Method

When you create a dictionary, you will need to use the items (Values) of this dictionary, so you need to get that value. A simple method called get() can perform this action. Let’s see an example:

# Create a Dictionary
Capitals = {"Germany":"Berlin", "UK":"London", "USA":"Washington"}

# Get The Dictionary Value
Value = Capitals.get("Germany")

# Print The Dictionary Value
print(Value)

You will get the key “Berlin” when you execute the command because tell python to get the value for the key “Germany” when we’ve used the get() method. 

3. Values() & Keys() Methods

We’ve learned before that when you write a dictionary, it will be like key:value, and if you want to get all the keys and values of it, then you use these two commands:

# Get The Dictionay Values
print(Capitals.values())

# Get The Dictionay Keys
print(Capitals.keys())

The above two methods will access the values and keys of your dictionary and print them on your screen. Try them, and you will better understand the dictionary in python.

4. Copy() Method

You can copy the entire dictionary and make another one with the same items. This action can be helpful sometimes when you try some functions on your dictionary without changing the original one. Let us see an example:

# Create a Dictionary
Capitals = {"Germany":"Berlin", "UK":"London", "USA":"Washington"}

# Create a Dictionary Copy
Capitals2 = Capitals.copy()

# Print The New Created Dictionary
print(Capitals2)

You will get the same original dictionary when you execute this code, although we printed the newly created one.

5. Update() Method

You also change the dictionary items using the built-in method in python dictionary called the update() method. Let’s see how you can change the values of your dictionary:

# Create Two Dictionaries
Capitals = {"Germany":"Berlin", "UK":"London", "USA":"Washington"}
New_Dic = {"Germany":"Moscow"}

# Update The Dictionary
Capitals.update(New_Dic)

# Print The Dictionay
print(Capitals)

We first created another dictionary called New_Dic, so we use the new value we want to change and apply it to the original one, the Capital dictionary. After using the update() method, you can see that the original dictionary value that was Berlin now is Moscow.

6. Pop() Method

This method, called pop(), is very similar to the previous one called the get() method, which gives you the value based on the given key. But this new one will give you the value based on the given key and remove both key and value from your dictionary. Let’s see an example:

# Create a Dictionary
Capitals = {"Germany":"Berlin", "UK":"London", "USA":"Washington"}

# Get The Dictionary Value
Value = Capitals.pop("Germany")

# Print The Dictionary Value
print(Value)

# Print The Dictionary
print(Capitals)

The pop() method works the same as the get() method, but when you print the dictionary again, you won’t get the key & value you’ve accessed these items.

7. Popitem() Method

This popitem() method is similar to the previous one. Still, instead of specifying the value you want to return and removing these items after that, it will give you the last key:value items of your dictionary and remove them afterward. Let’s see an example:

# Create a Dictionary
Capitals = {"Germany":"Berlin", "UK":"London", "USA":"Washington"}

# Get The Last Dictionary Value
Value = Capitals.popitem()

# Print The Dictionary Value
print(Value)

# Print The Dictionary
print(Capitals)

When you execute the code, you will see that the last item was printed first and then removed. The popitem() method doesn’t need any parameter like the previous one.

8. Items() Method

The dictionary syntax seems weird a little bit, and maybe you get confused about where is the value of this key. Therefore, you can use a method called items() to show you every key and its corresponding value in a simple syntax. Let’s see an example:

# Create a Dictionary
Capitals = {"Germany":"Berlin", "UK":"London", "USA":"Washington"}

# Print All Dictionary Items
print(Capitals.items())

It will put every key with its corresponding value inside a tuple, so it will be evident for you to know every key and its value.

9. Fromkeys() Method

You can create one from given keys and value and combine them using the fromkeys() method. Let’s see an example of this:

# The Dictionary Keys
Keys = {"G", "E", "R", "M", "A", "N", "Y"}

# The Dictionary Value
Value = "Berlin"

# Create The Dictionary
Dictionary = dict.fromkeys(Keys, Value)

# Print The Dictionary
print(Dictionary)

First, create the kays without their values, and we specify the Berlin value and combine them using the dict.fromkeys() function. Finally, print the newly created one, and you can notice that the order of the keys in the new one is not the same as the first one.

Conclusion

Thanks for reading! These methods are the most used ones in the python dictionary, and they can help you a lot in making your work more productive instead of writing a whole code for making the same action with a straightforward command.

Delete Files & Directories In Python
List Methods In Python
Scroll to Top