Dictionary
Store multiple values under a single variable na
me..
Key and value
Ordered, changeable, Not allow duplicates
Ex:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
print(thisdict)
To print a particular
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
Dictionary length
To determine how many items a dictionary has, use the len
() function:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
print(len(thisdict))
Data types
The values in dictionary items can be of any data type:
thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"]
Type()
Print the data type of a dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
print(type(thisdict))
The dict() Constructor
It is also possible to use the dict() construct
or to make a dictionary.
thisdict = dict(name = "John", age = 36, country = "Norway")
print(thisdict)
Accessing Items
You can access the items of a dictionary by referring to its k
ey name, inside square brackets:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = thisdict["model"]
Get method
There is also a method called get() that will give you the same re
sult:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = [Link]("model")
print(x)
Get key
The keys() method will return a list of all the keys in th
e dictionary.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = [Link]()
print(x)
Add item
Add a new item to the original dictionary, and see that the keys li
st gets updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = [Link]()
print(x) #before the change
car["color"] = "white"
print(x) #after the change
Get Values
The values() method will return a list of all the values in the dicti
onary.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = [Link]()
print(x)
Make a change in the original dictionary, and see that the values list gets upd
ated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = [Link]()
print(x) #before the change
car["year"] = 2020
print(x) #after the change
Check if Key Exists
To determine if a specified key is present in a dictionary use the i
n keyword:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictiona
ry")
Removing Items
There are several methods to remove items from a dictionary:
Pop()
The pop() method removes the item with the specified key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
[Link]("model")
print(thisdict)
Pop item()
The popitem() method removes the last inserted item..
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
[Link]()
print(thisdict)
Del()
The del keyword removes the item with the specified
key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
del thisdict["model"]
print(thisdict)
The del keyword can also delete the dictionary completely:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thi
sdict" no longer exists.
Clear()
The clear() method empties the dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
[Link]()
print(thisdict)
Loop Through a Dictionary
You can loop through a dictionary by using a for loop.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in thisdict:
print(x)
Values()
You can also use the values() method to return values of a dictio
nary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in [Link]():
print(x)
Keys()
You can use the keys() method to return the keys of a dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in [Link]():
print(x)
Items()
Loop through both keys and values, by using the items() method:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
for x, y in [Link]():
print(x, y)
Copy()
Make a copy of a dictionary with the copy() method:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
mydict = [Link]()
print(mydict)
Dict
Make a copy of a dictionary with the dict() function:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
mydict = dict(thisdict)
print(mydict)
Nested Dictionaries
A dictionary can contain dictionaries, this is called nested dicti
onaries.
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
print(myfamily)