Skip to content

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 {}:

empty_dict = dict()

# or

empty_dict = {}

Creating a Dictionary

Let's create a dictionary representing a network device, instead of an empty one:

Create a dictionary with contents
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:

Access a value using the key
print(device["hostname"])  # Output: router1

Attempting to access a non-existent key raises a KeyError:

Access a non-existent key
print(device["location"])  # Raises KeyError

Alternatively, you can use the get() method, which returns None if the key does not exist:

Access a value using the get() method
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:

Change a value
device["version"] = "16.12.5"

Adding Key-Value Pairs

You can add new key-value pairs to the dictionary:

Add a new key-value pair
device["location"] = input("Where is your device? ")

Removing Key-Value Pairs

You can remove key-value pairs using the del statement:

Remove a key-value pair
del device["uptime"]

Displaying the Dictionary

Use the pprint module to print the dictionary in a readable format:

Print the dictionary
from pprint import pprint
pprint(device)

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.