CCNP Automation 350-901 · Start here · Python 2

Python 2: lists, dictionaries, loops & decisions

Store many devices in a list, describe each one with a dictionary, repeat work with for loops and let the script decide with if/elif/else. Then loop over three live routers and flag the one with a down interface.

33 min read9 chapters2 labs15 quiz7 scenarios15 interview Q&A

This first module is free: read the lesson and take the quiz. Labs need an account: sign up free to run up to 3 hands-on labs.

Log inStart free trial
This reading material cannot be printed. Read it on config.nwkings.com.
01

Python 2: what you will learn and the big picture

What you will learn. In "Python 1" you stored one value in one variable. Real networks have many devices, so now you will store many values and let the script repeat work and decide. You will learn lists (many values in order), dictionaries (a value looked up by name), for loops (do it for every item), and if / elif / else (do it only when a condition is true). In the labs you build a small device inventory and then loop over three live routers, count their up interfaces and flag one that has a down interface.

Prerequisites. The module "Python 1": variables, strings, f-strings, comparisons and reading an error message. If the word f-string worries you, revisit that module first.

Analogy: a spreadsheet

Think of the inventory spreadsheet your team already keeps. A list is one column: every router IP, in order. A dictionary is one row with headers: name, IP, role, site and the values next to them. A for loop is "go down the rows, one by one". An if is a conditional rule: "if role is core, colour the row red". With only those four ideas you can read almost any structured data a network script meets.

Lista column of IPsDictionarya row: key to valuefor loopevery row in turnif / elsechoose what to do

The four ideas of this module: store, label, repeat, decide.

What the finished labs produce

Lab 1 turns a dictionary of four devices (plus one you add) into three small reports. This is the real output of the finished inventory.py:

AUTO> python3 inventory.py
All devices:
  PUN-CORE-01 -> 10.10.0.1
  PUN-ACC-01 -> 10.10.0.11
  BLR-CORE-01 -> 10.20.0.1
  BLR-ACC-01 -> 10.20.0.11
  HYD-ACC-01 -> 10.30.0.11
Total: 5
Core devices:
  PUN-CORE-01
  BLR-CORE-01
Per site: {'Pune': 2, 'Bengaluru': 2, 'Hyderabad': 1}

Lab 2 logs in to three routers, reads the interface table of each, counts the up interfaces and warns about any interface that is down (but not about a port that was shut on purpose):

AUTO> python3 check_up.py
MUM-R1: 2 interfaces up
MUM-R2: 3 interfaces up
MUM-R3: 2 interfaces up
  WARNING MUM-R3: down -> ['GigabitEthernet0/2']
Check finished

By the end of the module you will have written the lines that make this output appear.

The nine chapters

  1. Big picture (this chapter)
  2. Lists
  3. Dictionaries
  4. Nested data: an inventory
  5. for loops and range()
  6. Decisions: if / elif / else
  7. Counting and collecting (Lab 1)
  8. Loop over routers (Lab 2)
  9. Summary and checklist

Common mistake. Skipping straight to the labs. The labs ask you to edit specific line numbers with exact indentation; a quick read of chapters 2 to 6 saves a lot of frustration.

Exam trap. The 350-901 exam is full of lists and dictionaries, because API answers (JSON) are exactly nested lists and dictionaries. If you can read a loop over a dictionary, you can read most API-handling code on the exam.

The spreadsheet that became code

A team kept 300 devices in a spreadsheet and copied rows into scripts by hand. They moved the same columns into a Python dictionary, one entry per device. Now one loop can answer "which core devices are in Pune?" and the data is checked into version control instead of living on one laptop.

Lesson: data in lists and dictionaries is something a program can search and count.

"Why are lists and dictionaries so important for network automation?"

Device inventories, parsed show output and API responses (JSON) are all made of nested lists and dictionaries. Lists hold ordered collections such as device IPs; dictionaries hold labelled fields such as an interface's IP and status. Loops and conditions then process them.

Key takeaways

  • A list is many values in order; a dictionary looks a value up by its key.
  • A for loop repeats work for every item; if / elif / else chooses what to do.
  • Labs: build an inventory, then loop over three routers and flag a down interface.
  • These shapes are exactly what API data (JSON) looks like.

Upgrade to keep reading

The free plan lets you read chapter 1 of this module. Upgrade to open the remaining chapters.

  1. Lists: many values in one variable
  2. Dictionaries: key to value, like an ARP table
  3. Nested data: an inventory in a dictionary
  4. for loops and range(): do it for every item
  5. Decisions: if / elif / else
  6. Counting and collecting: Lab 1, the inventory
  7. Loop over routers: Lab 2, the up-interface check
  8. Summary and exam checklist

Join the community