Guide Python Intermediate

10.15 Lists in DevOps

Lists as the natural in-memory shape of file and log contents, plus real infrastructure patterns -- server inventories, filtering Amazon EC2 instances, Docker container checks, Kubernetes pod filtering, and log-severity filtering.

3 min read

Lists and File Handling

Lists are the natural in-memory representation of file contents — one element per line or row.

Iterating an open file object yields one line at a time; collecting them into a list, with .strip() to remove trailing newlines, is a standard pattern.

with open("servers.txt") as f:
    lines = [line.strip() for line in f]

>>> lines
['a', 'b', 'c']

Writing is the reverse: looping over a list and writing each item as its own line.

items = ["a", "b", "c"]
with open("output.txt", "w") as f:
    for item in items:
        f.write(item + "\n")

The csv module reads a file into a list of lists (or a list of dicts with DictReader) — one inner list or dict per row.

>>> import csv
>>> with open("data.csv") as f:
...     rows = list(csv.reader(f))
...
>>> rows
[['name', 'age'], ['Alice', '30']]

Server Inventory

The simplest and most common case: a flat list of hostnames to iterate over for health checks, deployments, or config pushes.

>>> servers = ["web01", "web02", "db01"]
>>> for server in servers:
...     print(f"Checking {server}")
Checking web01
Checking web02
Checking db01

Filtering Amazon EC2 Instances

boto3’s describe_instances() returns nested lists of dicts — list comprehensions (see 10.9 Traversing and List Comprehensions) are the idiomatic way to filter them, such as isolating only running instances. Full working scripts for this pattern live in Python Scripting, including the EC2 Untagged Instances Alert.

instances = [
    {"id": "i-001", "state": "running"},
    {"id": "i-002", "state": "stopped"},
]
>>> running = [i["id"] for i in instances if i["state"] == "running"]
>>> running
['i-001']

Docker

A list of running container names, checked with the in operator for quick presence tests.

>>> containers = ["nginx", "redis", "postgres"]
>>> "nginx" in containers
True

Kubernetes

Filtering pod names by prefix (deployment name) using a list comprehension with str.startswith() (see 9.7 Common String Methods).

>>> pods = ["web-abc123", "web-def456", "api-ghi789"]
>>> web_pods = [p for p in pods if p.startswith("web-")]
>>> web_pods
['web-abc123', 'web-def456']

Logs

Filtering a list of log lines down to just the ones matching a severity level — the same comprehension pattern used throughout this section.

>>> log_lines = ["INFO ok", "ERROR fail", "INFO ok"]
>>> errors = [l for l in log_lines if "ERROR" in l]
>>> errors
['ERROR fail']

Packages

Extracting just the package names from a requirements.txt-style list of pinned versions.

>>> packages = ["requests==2.28.0", "flask==2.0.1"]
>>> names = [p.split("==")[0] for p in packages]
>>> names
['requests', 'flask']

Quick Interview Answer

“Lists are the natural shape for anything read line-by-line or row-by-row: log files, CSV rows, server hostnames, boto3 API results. The recurring pattern across almost every DevOps list use case is a filtering comprehension — [x for x in collection if condition] — whether that’s isolating running Amazon EC2 instances by state, matching Kubernetes pod names by deployment prefix, or pulling error lines out of a log. The in operator handles quick presence checks, like confirming an expected Docker container is currently running.”

Common Mistakes

  • Loading an entire multi-gigabyte log file into a list with f.readlines() when a streaming line-by-line for line in f: loop would avoid holding the whole thing in memory at once.
  • Filtering Amazon EC2 or Kubernetes results with a manual loop and .append() instead of the more idiomatic (and often faster) list comprehension.
  • Treating csv.reader()’s output rows as already-typed values — every field comes back as a str, even numeric-looking ones; see 8.1 Introduction to Type Conversion.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form