Python dictionary is used to store data in key-value pairs. In this chapter, you will learn about Python dictionaries, their features, and how to use them with examples.
A dictionary is an unordered and mutable collection of data stored in key-value pairs. Each key is unique and is used to access its corresponding value.
Dictionaries are useful for storing related data and retrieving values using keys.

Let us take a look at a simple example of a dictionary.
Output:
{1: 'Learn', 2: 'Python', 3: 'from', 4: 'Tpoint', 5: 'Tech'}Explanation:
In the above example, we have created a simple dictionary consisting of multiple 'key: value' pairs.
As we can observe, a dictionary in Python is a mapping data type where the value of one object maps to another. In order to establish the mapping between a key and a value, we have used the colon ':' symbol between the two.
A dictionary is a data type with the following characteristics:
We can create a dictionary by enclosing the sequence of 'key: value' pairs with curly braces separated by commas. As an alternate option, we can use the Python's built-in dict() function.
Here is a simple example showing both ways of creating a dictionary.
Output:
Empty Dictionary: {}
Dictionary 1 (created using {}): {'name': 'Lucy', 'age': 19, 'city': 'New Jersey'}
Dictionary 2 (created using dict()): {'name': 'John', 'age': 21, 'city': 'Havana'}
Explanation:
The above example shows different ways to create dictionaries in Python. We have also seen how to create an empty dictionary.
Note: The dict() function can also be used to transform an existing data type into a dictionary.
We can access the value of a dictionary item by enclosing that particular key with square brackets '[]'. Another way to access dictionary items is by the use of the get() method.
The following is a simple example showing the ways to access dictionary items.
Output:
Person's Details Name: Sachin Age: 18 Gender: male Profession: student
Explanation:
Here, we have accessed the different values of the dictionary items using the square brackets and get() method.
The dictionary is a mutable data type that allows us to add an item to it. It can be done by assigning a value to a new key.
Let us take a look at a simple example showing how to add items to a Python dictionary.
Output:
Given Dictionary: {'name': 'Sachin', 'age': 18, 'gender': 'male', 'profession': 'student'}
Updated Dictionary: {'name': 'Sachin', 'age': 18, 'gender': 'male', 'profession': 'student', 'country': 'India'}
Explanation:
In this example, we have added a new 'key: value' pair to the dictionary using the assignment operator.
Python offers multiple ways to remove items from a given dictionary, such as:
Here is an example showing the use of different methods to remove items from a Python dictionary.
Output:
Given Dictionary: {'name': 'Sachin', 'age': 18, 'gender': 'male', 'profession': 'student', 'country': 'India'}
Updated Dictionary (Removed 'age'): {'name': 'Sachin', 'gender': 'male', 'profession': 'student', 'country': 'India'}
Updated Dictionary (Removed 'gender'): {'name': 'Sachin', 'profession': 'student', 'country': 'India'}
Popped Value: male
Updated Dictionary (Removed last item): {'name': 'Sachin', 'profession': 'student'}
Popped Item: ('country', 'India')
Update Dictionary (Removed all items): {}
Explanation:
In this example, we are given a dictionary. We have used several methods like del keyword, pop(), popitem(), and clear() methods to remove the items from the dictionary.
We can change the values of an item in the dictionary by referring to its key.
Let us take a simple example to understand how to change dictionary items in Python.
Output:
Given Dictionary: {'name': 'Sachin', 'age': 18, 'gender': 'male', 'profession': 'student', 'country': 'India'}
Updated Dictionary: {'name': 'Sachin', 'age': 20, 'gender': 'male', 'profession': 'developer', 'country': 'India'}
Explanation:
In this example, we have used the assignment operator to change the value of existing keys in the given dictionary. As a result, the dictionary items are updated.
Starting from Python 3.7, a dictionary is an ordered collection of items; therefore, it maintains the order of its items. We can iterate through dictionary keys using the 'for' loop.
Let us take an example to demonstrate how to iterate key through a dictionary.
Output:
Items in Dictionary: Name -> Sachin Age -> 18 Gender -> Male Profession -> Student Country -> India
Explanation:
In the above example, we have used the 'for' loop to iterate through the keys in dictionary and accessed the value for each key.
In order to find the length of a dictionary, we can use Python's built-in function called len(). This function will return the total number of 'key: value' pairs present in a dictionary, allowing us to determine the size of the dictionary efficiently.
Let us see the following example showing the use of the len() function in determining the length of a Python dictionary.
Output:
Given Data: {'John': 'Sr. Software Developer', 'Irfan': 'UI/UX Designer', 'Lucy': 'Human Resource Manager', 'Peter': 'Team Lead', 'Johnson': 'Business Developer'}
Size of Data: 5
Explanation:
In the above example, we have used the len() function in order to find out how many items are in the given dictionary.
We can use the 'in' or 'not in' operators in order to check whether a key exists in a dictionary.
Here's a simple example that shows how to see if a specified key is part of a dictionary in Python.
Output:
Is 'fruit' a member of 'dict_y'?: True Is 'beverage' a member of 'dict_y'?: False Is 'beverage' NOT a member of 'dict_y'?: True
Explanation:
In this example, we have used the 'in' and 'not in' operators to check if the specified keys are present in the given dictionary. The 'in' operator returns the Boolean value after checking if the key exists in the dictionary, whereas the 'not in' operator returns the Boolean value after checking if the key does not exist in it.
Python provides several dictionary methods to work with dictionary data. These methods help us add, update, delete, and access elements in a dictionary.
Some commonly used dictionary methods are as follows:
| Dictionary Method | Description |
|---|---|
| get() | This method returns the value associated with a specific key. |
| update() | This method is utilized to add a new item to the dictionary or update the value of an existing key. |
| copy() | This method is utilized to return a copy of the dictionary. |
| pop() | This method removes the item with the given key from the dictionary. |
| popitem() | This method is utilized to return the last inserted key and value as a tuple. |
| clear() | This method removes all items from the dictionary. |
| keys() | This method returns all the keys in the dictionary. |
| values() | This method is utilized to return all the values in the dictionary. |
We request you to subscribe our newsletter for upcoming updates.