Dictionaries
The following is a soft introduction to dictionaries in Python for network engineers.
Dictionaries are a collection of key-value pairs. They are unordered and mutable, defined by curly braces {} and
key-value pairs separated by a colon :. Mutable means that it can be changed after it's been created. Just like a list.
A dictionary is sometimes referred to as a hash, map, or associative array in other programming languages. Python calls it a
dictionary or dict. Similar data structures are called Mapping in Python. A mapping is a collection of key-value pairs
where each key is unique.
The class name for a dictionary is dict. You can create an empty dictionary using the dict() constructor or by using curly
braces {}:
Creating a Dictionary¶
Let's create a dictionary representing a network device, instead of an empty one:
device = {
"hostname": "router1",
"vendor": "Cisco",
"model": "ISR4451",
"os": "IOS-XE",
"version": "16.12.4",
"uptime": "5 days",
}
As you can see in the example above, the dictionary device contains key-value pairs for various attributes of a network device.
The keys are unique and are used to access the corresponding values. The left-most value in each pair is the key, and the
right-most value is the value.
In the case of the device dictionary, the keys are:
"hostname""vendor""model""os""version""uptime"
The values associated with these keys are:
"router1""Cisco""ISR4451""IOS-XE""16.12.4""5 days"
Accessing Values¶
You can access values in a dictionary using the key:
Attempting to access a non-existent key raises a KeyError:
Alternatively, you can use the get() method, which returns None if the key does not exist:
print(device.get("vendor")) # Output: Cisco
print(device.get("location")) # Output: None
print(device.get("location", "Data Center")) # Output: Data Center
Modifying Values¶
You can change the value associated with a key:
Adding Key-Value Pairs¶
You can add new key-value pairs to the dictionary:
Removing Key-Value Pairs¶
You can remove key-value pairs using the del statement:
Displaying the Dictionary¶
Use the pprint module to print the dictionary in a readable format:
Summary¶
In this lesson, we covered the basics of dictionaries in Python, including how to create, access, modify, add, and remove key-value pairs. Dictionaries are a powerful tool for storing and managing data, especially in network engineering where device configurations and attributes need to be handled efficiently.
Key Points
- Dictionaries are unordered collections of key-value pairs.
- Use square brackets
[]to access values by key. - The
get()method is a safe way to access values. - Dictionaries are mutable, allowing for dynamic updates.
Try It Yourself
Create a dictionary for another network device and practice accessing and modifying its values. Experiment with adding new key-value pairs and removing existing ones.
Congratulations on completing this introduction to dictionaries! Keep practicing to become more comfortable with this essential Python data structure.