Dictionaries are ordered collection of data items. They store multiple items in a single variable.
Creating Dictionaries
Dictionary items are key-value pairs that are separated by commas and enclosed within curly brackets {}
.
Example:
info = {'name':'John', 'profession':'Developer', 'age':21}
print(info)
Output:
{'name': 'John', 'profession': 'Developer', 'age': 21}
Here, name, profession and age
are keys and John, Developer and 21
are values.
Accessing Dictionary items
You can access dictionary items in the following ways:
Accessing single values
Values in a dictionary can be accessed using keys
. You can access dictionary values by mentioning keys either in square brackets []
or by using get()
method.
Example:
info = {'name':'John', 'profession':'Developer', 'age':21}
print(info['name'])
print(info.get('name'))
Output:
John
John
The difference between []
and get()
is that when you try to access the key which is not present in the dictionary then []
gives an error while get()
gives None
.
For example:
info = {'name':'John', 'profession':'Developer', 'age':21}
print(info['experience']) # KeyError: 'experience'
print(info.get('experience')) # None
Accessing multiple values
You can access all the values of a dictionary using values()
method.
Example:
info = {'name':'John', 'profession':'Developer', 'age':21}
print(info.values())
Output:
dict_values(['John', 'Developer', 21])
Accessing keys
You can access all the keys of a dictionary using keys()
method.
Example:
info = {'name':'John', 'profession':'Developer', 'age':21}
print(info.keys())
Output:
dict_keys(['name', 'profession', 'age'])
Accessing key-value pairs
You can access all the key-value pairs of a dictionary using items()
method.
Example:
info = {'name':'John', 'profession':'Developer', 'age':21}
print(info.items())
Output:
dict_items([('name', 'John'), ('profession', 'Developer'), ('age', 21)])
Checking if a Key Exists
You can check whether a specific key is present in a dictionary using the in
keyword.
Example:
info = {'name': 'John', 'profession': 'Developer', 'age': 21}
print('name' in info)
print('salary' in info)
Output:
True
False
This is useful when you want to avoid errors before accessing a key.
Dictionary Methods
Python provides the following built-in methods for dictionary manipulation:
update()
The update()
method is used to update the value of the key provided to it.
If the item already exists in the dictionary then it will update the item, otherwise it creates a new key-value pair.
Example:
info = {'name':'John', 'profession':'Developer', 'age':21}
print(info)
info.update({'age':32})
info.update({'lastName':"Wick"})
print(info)
Output:
{'name': 'John', 'profession': 'Developer', 'age': 21}
{'name': 'John', 'profession': 'Developer', 'age': 32, 'lastName': 'Wick'}
clear()
The clear()
method is used to remove all the items from the list.
Example:
info = {'name':'John', 'profession':'Developer', 'age':21}
info.clear()
print(info)
Output:
{}
pop()
The pop()
method is used to remove that key-value pair whose key is passed as a parameter.
Example:
info = {'name':'John', 'profession':'Developer', 'age':21}
info.pop('profession')
print(info)
Output:
{'name': 'John', 'age': 21}
popitem()
The popitem()
method is used to remove the last key-value pair from the dictionary.
Example:
info = {'name':'John', 'profession':'Developer', 'age':21}
info.popitem()
print(info)
Output:
{'name': 'John', 'profession': 'Developer'}
del
The del
keyword is used to remove a dictionary item.
Example:
info = {'name':'John', 'profession':'Developer', 'age':21}
del info['profession']
print(info)
Output:
{'name': 'John', 'age': 21}
If key is not provided, then the del
keyword will delete the entire dictionary.
Example:
info = {'name':'John', 'profession':'Developer', 'age':21}
del info
print(info)
Output:
NameError: name 'info' is not defined
Merge Dictionaries
In the latest update of python (Python 3.9), you can use |
operator to merge two dictionaries.
info1 = {'name': 'John', 'age': 21}
info2 = {'profession': 'Developer', 'city': 'New York'}
merged = info1 | info2
print(merged)
Output:
{'name': 'John', 'age': 21, 'profession': 'Developer', 'city': 'New York'}
👉 Next tutorial: Python Exception Handling