Dictionary
Dictionary
• Python dictionary represents a mapping between a key and a value. In simple terms, a Python dictionary
can store pairs of keys and values. Each key is linked to a specific value.
• Once stored in a dictionary, you can later obtain the value using just the key.
• For example, consider the Phone lookup, where it is very easy and fast to find the phone number(value)
when we know the name(Key) associated with it.
Creating a dictionary
• Using curly brackets: The dictionaries are created by enclosing the comma-separated
Key: Value pairs inside the {} curly brackets. The colon ‘:‘ is used to separate the key
and value in a pair.
Example:
Empty Dictionary
When we create a dictionary without any elements inside the curly brackets then it will be an empty
dictionary.
emptydict = {}
print(type(emptydict))
# Output class 'dict'
• A dictionary value can be of any type, and duplicates are allowed in that.
• Keys in the dictionary must be unique and of immutable types like string, numbers, or
tuples.
1. Retrieve value using the key name inside the [] square brackets
2. Retrieve value by passing key name as a parameter to the get() method of a
dictionary.
Example
Use the following dictionary methods to retrieve all key and values at once
Method Description
Example
print(type(person.keys()))
# Output class 'dict_keys'
print(type(person.values()))
# Output class 'dict_values'
Iterating a dictionary
We can iterate through a dictionary using a for-loop and access the individual keys and their
corresponding values.
key : value
name : Jessa
country : USA
telephone : 1178
key : value
name Jessa
country USA
telephone 1178
In order to find the number of items in a dictionary, we can use the len() function.
We can add new items to the dictionary using the following two ways.
• Using key-value assignment: Using a simple assignment statement where value can
be assigned directly to the new key.
• Using update() Method: In this method, the item passed inside the update() method
will be inserted into the dictionary. The item can be another dictionary or any iterable
like a tuple of key-value pairs.
Example
There are several methods to remove items from the dictionary. Whether we want to remove the
single item or the last inserted item or delete the entire dictionary, we can choose the method to be
used.
Method Description
pop (key [, d]) the key and return its value. If the key is not
Example
person = {'name': 'Jessa', 'country': 'USA', 'telephone': 1178, 'weight': 50, 'height': 6}
In order to check whether a particular key exists in a dictionary, we can use the keys() method
and in operator. We can use the in operator to check whether the key is present in the list of keys
returned by the keys() method.
In this method, we can just check whether our key is present in the list of keys that will be returned
from the keys() method.
Let’s check whether the ‘country’ key exists and prints its value if found.
Copy a Dictionary
Note: When you set dict2 = dict1, you are making them refer to the same dict object, so when
you modify one of them, all references associated with that object reflect the current state of
the object. So don’t use the assignment operator to copy the dictionary instead use
the copy() method.
Example
print(dict1)
# Output {'Jessa': 90, 'Emma': 55}
Nested dictionary
Nested dictionaries are dictionaries that have one or more dictionaries as their members.
Example
# Display dictionary
print("person:", person)
City: Houston
Person details
name: Jessa
company: Google
Person Address
state: Texas
city : Houston
Sort dictionary
The built-in method sorted() will sort the keys in the dictionary and returns a sorted list. In
case we want to sort the values we can first get the values using the values() and then sort
them.
Example