Python Dictionary

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

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 {}. https://vimeo.com/430039987 Here is a quick example: This "routers" dictionary has two items: Item 1: Key: string "

Here is a quick example:

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:

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:

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 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”

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”:

This shows us the string with the IP address. Let’s retrieve key “SW2” from the dictionary “switches”:

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:

Above, we see the string with our new IP address.

Add Item

We can add an item to an existing dictionary. For example:

Remove an Item

Removing an item is also no problem. There are two options. The first one is the pop method:

This removes the item and reports the item that is removed.

Another way is the del statement:

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:

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.

Check if an item exists

We can also check whether an item exists in the dictionary:

The keyword in is a membership operator, and returns a boolean value (True or False). Later, in the if/elif/else lesson, we’ll see how we can use these values to make decisions in our code.

Conclusion

You have now learned how to use Python dictionaries:

  • Dictionaries use a key-value pair. The key is unique.
  • We create a dictionary by using curly brackets {}.
  • You can use many data types for the value including strings, integers, lists, and even other dictionaries.
  • We can use membership operators to see if a key exists in the dictionary.
  • Dictionaries are mutable so you can add, modify, or delete items later.
  • The in membership operator lets us check if an item exists in a dictionary.

Ask a question or start a discussion by visiting our Community Forum