This article entitle Python Dictionary is a continuation of the previous article entitled Python Tuple.
Dictionary in Python is a collection ordered that start from the 3.7 Python version each item stores key-value pairs and these keys are unique identifiers which is associated with each value.
What is a Python dictionary?
In Python, the dictionary is an implementation of the data structure that is well known to be an associative array.
Further, a dictionary consists of a key-value pairs collection and each key-value pair maps each key to its associated array value.
Example
For instance, we want to create and store information about countries and their capitals. That’s the point that we need to create the Python Dictionary with the specific country names as the keys and the capital will be the values.
Keys | Values |
France | Paris |
Germany | Berlin |
Norway | Oslo |
Philippines | Manila |
India | New Delhi |
Create a Python dictionary
The following is a Python program to create the dictionary.
cityCapital = {"France": "Paris", "Germany": "Berlin", "Norway": "Oslo", "Philippines": "Manila", "India": "New Delhi"} print(cityCapital)
Output
{'France': 'Paris', 'Germany': 'Berlin', 'Norway': 'Oslo', 'Philippines': 'Manila', 'India': 'New Delhi'}
We can also create a dictionary with different data types lists.
Example
# dictionary with keys and values of different data types numberKeys = {100: "One Hundred", 200: "Two Hundred", 300: "Three Hundred"} print(numberKeys)
Output
{100: 'One Hundred', 200: 'Two Hundred', 300: 'Three Hundred'}
Add elements in the Python dictionary
We can add items to a Python dictionary method with the use of the name of the dictionary with the square brackets [] symbol.
Example
cityCapital = {"France": "Paris", "Germany": "Berlin", "Norway": "Oslo", "Philippines": "Manila", "India": "New Delhi"} print("Initial Dictionary: ",cityCapital) cityCapital["Qatar"] = "Doha" print("Updated Dictionary: ",cityCapital)
Output
Initial Dictionary: {'France': 'Paris', 'Germany': 'Berlin', 'Norway': 'Oslo', 'Philippines': 'Manila', 'India': 'New Delhi'} Updated Dictionary: {'France': 'Paris', 'Germany': 'Berlin', 'Norway': 'Oslo', 'Philippines': 'Manila', 'India': 'New Delhi', 'Qatar': 'Doha'}
Update value in Python dictionary
To update an item in the dictionary we can use the curly braces {} and square brackets [] symbol with an associated value with a particular dict key.
Example
studentIdentification = {1111: "Jude", 1112: "Glenn", 1113: "Paul", 1114: "Adones"} print("Initial Dictionary: ", studentIdentification) studentIdentification[1112] = "Adones" print("Updated Dictionary: ", studentIdentification)
Output
Initial Dictionary: {1111: 'Jude', 1112: 'Glenn', 1113: 'Paul', 1114: 'Adones'} Updated Dictionary: {1111: 'Jude', 1112: 'Adones', 1113: 'Paul', 1114: 'Adones'}
Accessing elements in a dictionary in Python
To access the value in the dictionary we use the keys to access the corresponding values.
Example
studentIdentification = {1111: "Jude", 1112: "Glenn", 1113: "Paul", 1114: "Adones"} print(studentIdentification[1112]) print(studentIdentification[1113])
Output
Glenn Paul
Removing elements from a dictionary
To remove or delete a key-value pair in the element from a dictionary we can use the (del statement).
Example
studentIdentification = {1111: "Jude", 1112: "Glenn", 1113: "Paul", 1114: "Adones"} print("Initial Dictionary: ", studentIdentification) del studentIdentification[1111] print("Updated Dictionary ", studentIdentification)
Output
Initial Dictionary: {1111: 'Jude', 1112: 'Glenn', 1113: 'Paul', 1114: 'Adones'} Updated Dictionary {1112: 'Glenn', 1113: 'Paul', 1114: 'Adones'}
Iterating through a dictionary
To iterate through each key in the dictionary we can use the (for loop).
Example
# Iterating through a Dictionary iterate = {1: 5, 3: 19, 5: 65, 7: 78, 9: 111} for i in iterate: print(iterate[i])
Output
5 19 65 78 111
Python dictionary membership test
To test if the existent key is in the dictionary or not we can use the (in) keyword. Take Note that the membership test is for only the keys and not for values in the dictionary elements.
Example
# Membership Test for Dictionary Keys iterate = {1: 5, 3: 19, 5: 65, 7: 78, 9: 111} # Output: True print(1 in iterate) # prints True print(2 not in iterate) # prints True # membership tests for key only not value print(49 in iterate) # prints false
Output
True True False
Methods of Python dictionary
The following table below is the list of methods in the dictionary
Function | Description |
all() | It will return True if all the keys in the dictionary are TRUE or the dictionary is empty. |
any() | It will return TRUE if any key in the dictionary is TRUE and if the dictionary is empty it will return FALSE. |
len() | It will return the length or the number of items in the dictionary in Python. |
sorted() | It will return a new set of a sorted list of keys in the dictionary |
clear() | It will remove all the items or elements from the dictionary |
keys() | It will return a new set of objects in dictionary keys. |
values() | It will return a new set of objects in dictionary values. |
Summary
In summary, you have read about Python Dictionary. We also discussed in this article what is a Python dictionary, create a dictionary, add elements, update value, access elements, remove elements, iterate through the dictionary, and the methods of a dictionary.
I hope this article could help you a lot to continue pursuing learning this powerful programming language.
If you want to learn more check out my previous and latest articles for more career-changing articles.