← Lessons/Lists, Dicts & Comprehensions

Lists, dicts & comprehensions

Almost every script you'll write holds a collection of things: a set of servers, a table of counts, a batch of rows. Two built-in structures carry most of that work, and a one-line idiom builds new ones from old. Your main.py builds up across the steps.

pythonautomationdata-structures

Interview prep

Reaching for a comprehension instead of a manual loop-and-append is a quick fluency signal.

When would you use a list comprehension over a for loop?

When you're building a new list by transforming or filtering an existing iterable — [f(x) for x in items if cond(x)]. It's more concise and usually faster than initializing an empty list and appending in a loop. For side effects (printing, writing) or complex logic, a plain loop is clearer.