Python Dictionary
Python Dictionary
N A
O N
T H O
Y TI
P IC
D
CREATE A DICTIONARY
Python dictionary is a collection of items, similar to lists and tuples.
However, unlike lists and tuples, each item in a dictionary is a key-
value pair (consisting of a key and a value).
We create a dictionary by placing key: value pairs inside curly
brackets {}, separated by commas. For example,
# creating a dictionary
country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
"England": "London"
}
# printing the dictionary
print(country_capitals)
Output
{'Germany': 'Berlin', 'Canada': 'Ottawa', 'England': 'London'}
Notes:
Dictionary keys must be immutable, such as tuples, strings, integers, etc.
We cannot use mutable (changeable) objects such as lists as keys.
We can also create a dictionary using a Python built-in function dict().
Using the dict() method to make a dictionary:
thisdict = dict(name = "John", age = 36, country = "Norway")
print(thisdict)
We can access the value of a dictionary item by placing the key inside
square brackets.
country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
"England": "London"
}
result = scores.get('Physics')
print(result) # 67
get() method returns:
the value for the specified key if key is in the dictionary.
None if the key is not found and value is not specified.
value if the key is not found and value is specified.
How does get() work for dictionaries?
person = {'name': 'Phill', 'age': 22}
print('Name: ', person.get('name'))
# value is provided
print('Salary: ', person.get('salary', 0.0))
Output
Name: Phill
Age: 22
Salary: None
Salary: 0.0
The keys of a dictionary must be unique. If there
are duplicate keys, the later value of the key
overwrites the previous value.
hogwarts_houses = {
"Harry Potter": "Gryffindor",
"Hermione Granger": "Gryffindor",
"Ron Weasley": "Gryffindor",
# duplicate key with a different house
"Harry Potter": "Slytherin"
}
print(hogwarts_houses)
Output
{'Harry Potter': 'Slytherin', 'Hermione Granger': 'Gryffindor', 'Ron Weasley':
'Gryffindor'}
Here, the key Harry Potter is first assigned to Gryffindor. However, there is a
second entry where Harry Potter is assigned to Slytherin.
As duplicate keys are not allowed in a dictionary, the last
entry Slytherin overwrites the previous value Gryffindor.
We can add an item to a dictionary by assigning a value to a new key.
country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
}
# add an item with "Italy" as key and "Rome" as its value
country_capitals["Italy"] = "Rome"
print(country_capitals)
print(country_capitals)
Output
{'Germany': 'Berlin', 'Italy': 'Rome', 'England': 'London'}
The update() method updates the dictionary with the
elements from another dictionary object or from an iterable
of key/value pairs.
marks = {'Physics':67, 'Maths':87}
internal_marks = {'Practical':48}
marks.update(internal_marks)
print(marks)
# Output: {'Physics': 67, 'Maths': 87, 'Practical': 48}
Note: The update() method adds element(s) to the dictionary if the key is not
in the dictionary. If the key is in the dictionary, it updates the key with the
new value.
update() When Tuple is Passed
dictionary = {'x': 2}
dictionary.update([('y', 3), ('z', 0)])
print(dictionary)
Output
{'x': 2, 'y': 3, 'z': 0}
Here, we have passed a list of tuples [('y', 3), ('z', 0)] to
the update() function. In this case, the first element of tuple is used as the
key and the second element is used as the value.