NetAuto Academy

Python for Network Engineers

Lists & Dictionaries for Network Data

Beginner14 min read

What you'll learn

  • What a list is, and when it's the right structure to use
  • What a dictionary is, and how it differs from a list
  • How to access values in each โ€” by position in a list, by key in a dictionary
  • How a list of dictionaries represents a realistic device inventory
  • Why this exact structure is what 'structured inventory' from the last module actually looks like in code
๐ŸŽ“ Teach This Lesson

Listen to a guided explanation of this lesson.

Spoken narration isn't supported in this browser.

Two structures, two different jobs

This is one of the most important lessons in this module, because almost every script you'll ever write for network automation is built on exactly two structures: a list, for a collection of similar things, and a dictionary, for structured facts about one thing.

A list is a collection of similar items. Three switch hostnames, in order:

device_list.py
devices = ["SW1", "SW2", "SW3"]

print(devices[0])   # SW1  โ€” the first item
print(devices[1])   # SW2  โ€” the second item

Square brackets create a list. Each item has a position, starting at 0 โ€” devices[0] is the first item, not the second. This trips people up once; after that it's just how Python counts.

A dictionary is structured information about one item โ€” a set of named facts, not just a bare list of values:

device_facts.py
device = {
  "hostname": "SW1",
  "ip": "10.10.10.1",
  "platform": "cisco_ios",
}

print(device["hostname"])   # SW1
print(device["ip"])         # 10.10.10.1

Curly braces create a dictionary. Each entry is a key ("hostname") paired with a value ("SW1"). Unlike a list, you access a dictionary's values by name โ€” device["hostname"] โ€” not by position, because there's no meaningful "first" or "second" fact about a device.

Quick checkConcept check
What's the key difference between a list and a dictionary?
Quick checkIdentify the correct code
Given devices = ["SW1", "SW2", "SW3"], which line correctly prints the second item, "SW2"?

The structure that matters most: a list of dictionaries

Here's where these two ideas combine into the shape almost every real inventory takes โ€” a list of dictionaries: many devices (the list), each with several named facts (a dictionary per device).

inventory.py
devices = [
  {
      "hostname": "SW1",
      "ip": "10.10.10.1",
      "platform": "cisco_ios",
  },
  {
      "hostname": "SW2",
      "ip": "10.10.10.2",
      "platform": "cisco_ios",
  },
]

print(devices[0]["hostname"])   # SW1
print(devices[1]["ip"])         # 10.10.10.2

devices is a list โ€” accessed by position, devices[0] and devices[1]. Each item in that list is itself a dictionary โ€” accessed by key, ["hostname"], ["ip"]. Combine the two and devices[0]["hostname"] reads as 'the hostname of the first device.'

  1. Inventorya list of devices
  2. Device 1 โ€” SW1hostname ยท ip ยท platform
  3. Device 2 โ€” SW2hostname ยท ip ยท platform

If this shape looks familiar, it should โ€” this is exactly what "Devices, Inventory & Source of Truth," two modules back, called structured inventory: not just a bare list of hostnames, but each device carrying its own hostname, management IP, and platform. That wasn't an abstract idea. This โ€” a list of dictionaries โ€” is what it actually looks like in code.

Quick checkScenario
Which Python structure is better suited for storing multiple attributes (hostname, IP, platform) of a single device?
Quick checkConcept check
In 'devices[0]["hostname"]', what does the [0] refer to, and what does ["hostname"] refer to?

Why this structure is worth learning properly

Every script in the rest of this module โ€” loops, conditions, functions, the capstone script โ€” works on data shaped exactly like this: a list of devices, each one a dictionary of facts. Once you're comfortable reading devices[0]["hostname"] and writing a list of dictionaries by hand, the rest of this module is mostly about doing things with that shape, not learning new shapes.

A quick way to tell them apart

If you're describing "many of the same kind of thing" (many switches, many VLANs, many interfaces), reach for a list. If you're describing "several different facts about one thing" (one switch's hostname, IP, and platform), reach for a dictionary. Most real inventory data โ€” as you just saw โ€” ends up needing both at once.

Key takeaways

  • A list holds a collection of similar items, accessed by position: devices[0], devices[1].
  • A dictionary holds structured facts about one thing, accessed by key: device["hostname"], device["ip"].
  • A list of dictionaries โ€” many devices, each a dictionary of facts โ€” is the shape almost every real device inventory takes in code.
  • This is the same 'structured inventory' idea from 'Network Automation Fundamentals,' now expressed as actual data you can write and read.
  • Every remaining lesson in this module works with data shaped exactly like this.