Lesson Contents
A Python dictionary can store data like a list or tuple but works with a key-value pair. The key is unique, the value doesn’t have to be. We create a dictionary with curly brackets {}.
Here is a quick example:
>>> routers = {"R1":"192.168.1.1","R2":"192.168.1.2"}
>>> print(routers)
Will show this output:
{'R1': '192.168.1.1', 'R2': '192.168.1.2'}
This “routers” dictionary has two items:
- Item 1:
- Key: string “R1”
- Value: string “192.168.1.1”
- Item 2:
- Key: string “R2”
- Value: string “192.168.1.2”
In the example above I used strings but you could also use something else. For example, I can use a list for the value:
>>> switches = {"SW1": ["172.16.1.1","3750"], "SW2": ["172.16.1.2","3850"]}
>>> print(switches)
Will show this output:
{'SW1': ['172.16.1.1', '3750'], 'SW2': ['172.16.1.2', '3850']}
The “switches” dictionary above has two items:
- Item 1:
- Key: string “SW1”
- Value: list with two items:
- String: “172.16.1.1”
- String: “3750”
- Item 2:
- Key: string “SW2”
- Value: list with two items:
- String: “172.16.1.2”
- String: “3850”
Nested Dictionary
We can also nest dictionaries:
>>> firewalls = {
"FW1" : {
"location" : "Amsterdam",
"software" : "IOS"
},
"FW2" : {
"location" : "New York",
"software" : "IOS-XE"
}
}
>>> print(firewalls)
Will show this output:
{'FW1': {'location': 'Amsterdam', 'software': 'IOS'}, 'FW2': {'location': 'New York', 'software': 'IOS-XE'}}
The “firewalls” dictionary has two items:
- Item 1:
- Key: string “FW1”
- Value: dictionary with two items:
- Item 1:
- Key: string “location”
- Value: string “Amsterdam”
- Item 2:
- Key: string “software”
- Value: string “IOS”
- Item 1:
- Item 2:
- Key: string “FW2”
- Value: dictionary with two items:
- Item 1:
- Key: string “location”
- Value: string “New York”
- Item 2:
- Key: string “software”
- Value: string “IOS-XE”
- Item 1:
Dictionaries (and nested dictionaries) are a great way to store structured data.
Access an Item
We can access an item in the dictionary by specifying the key. For example, let’s fetch the key “R1” from the dictionary “routers”:
>>> routers = {"R1":"192.168.1.1","R2":"192.168.1.2"}
>>> print(routers["R1"])
Will show this output:
192.168.1.1
This shows us the string with the IP address. Let’s retrieve key “SW2” from the dictionary “switches”:
>>> switches = {"SW1": ["172.16.1.1","3750"], "SW2": ["172.16.1.2","3850"]}
>>> print(switches["SW2"])
Will show this output:
['172.16.1.2', '3850']
This shows us our list with two strings.
Change Item
We can also change items in a dictionary. Let’s change the IP address of R1:
>>> routers = {"R1":"192.168.1.1","R2":"192.168.1.2"}
>>> print(routers["R1"])
192.168.1.1
>>> routers["R1"] = "192.168.1.20"
Will show this output:
192.168.1.20
Above, we see the string with our new IP address.
Add Item
We can add an item to an existing dictionary. For example:
>>> routers = {"R1":"192.168.1.1","R2":"192.168.1.2"}
>>> print(routers)
{'R1': '192.168.1.1', 'R2': '192.168.1.2'}
>>> routers["R3"] = "192.168.1.3"
>>> print(routers)
Will show this output:
{'R1': '192.168.1.1', 'R2': '192.168.1.2', 'R3': '192.168.1.3'}
Remove an Item
Removing an item is also no problem. There are two options. The first one is the pop method:
>>> routers = {"R1":"192.168.1.1","R2":"192.168.1.2", "R3":"192.168.1.3"}
>>> print(routers)
{'R1': '192.168.1.1', 'R2': '192.168.1.2', 'R3': '192.168.1.3'}
>>> routers.pop("R3")
'192.168.1.3'
>>> print(routers)
Will show this output:
{'R1': '192.168.1.1', 'R2': '192.168.1.2'}
This removes the item and reports the item that is removed.
Another way is the del statement:
>>> routers = {"R1":"192.168.1.1","R2":"192.168.1.2", "R3":"192.168.1.3"}
>>> print(routers)
{'R1': '192.168.1.1', 'R2': '192.168.1.2', 'R3': '192.168.1.3'}
>>> del routers["R2"]
>>> print(routers)
Will show this output:
{'R1': '192.168.1.1', 'R3': '192.168.1.3'}
The difference between the pop method and the del statement is that the pop method returns the value of the key that we delete. You can see this when you run the above examples in the interpreter. Here is an example:
>>> routers = {"R1":"192.168.1.1","R2":"192.168.1.2", "R3":"192.168.1.3"}
Will show this output:
>>> del routers["R2"]
>>> routers.pop("R3")
'192.168.1.3'
Above, we see that the interpreter doesn’t show us anything when we use the del statement but does show us the value (192.168.1.2) of key “R3” when we use the pop method.