Lists and loops: doing the same thing to many things
If you’ve ever dragged a formula down two hundred rows in a spreadsheet, you’ve already done the mental work behind this lesson — you just did it by hand, one row at a time. A list is where Python keeps many things in one labelled box, and a loop is how you tell it to run the same instruction down every row for you.
A list is just an ordered set of boxes
clients = ["Okafor & Sons", "Meridian Health Trust", "Brightwell Logistics"]
This is a list of three client names, in order. You can pull one out by its position — clients[0] is "Okafor & Sons", clients[1] is "Meridian Health Trust". Python counts positions from zero, not one, which trips up everyone the first time and no one after that.
Looping over the list
for client in clients:
print(f"Preparing engagement letter for {client}")
Read this as: “for each client in the list clients, do the indented line.” Python runs the print line once per name, automatically. The name you give the loop variable (client) is yours to choose — it’s just a label for “whichever item we’re on right now.”
Building a shortlist as you go
overdue = []
for client in clients:
if client == "Brightwell Logistics":
overdue.append(client)
print(overdue)
overdue = [] starts an empty list. Inside the loop, .append() adds an item to the end of it. Combined with an if, this is the pattern behind most real “go through this and pull out the ones that matter” tasks — chasing unpaid invoices, flagging missed deadlines, shortlisting files that need a second look.
Working with two lists side by side
Often you’ll have related information split across two lists — client names in one, something about each client in another, lined up by position. range(len(clients)) gives you the positions themselves (0, 1, 2…), so you can index into both lists with the same number:
clients = ["Okafor & Sons", "Meridian Health Trust", "Brightwell Logistics"]
overdue_days = [0, 45, 12]
for i in range(len(clients)):
print(f"{clients[i]}: {overdue_days[i]} days overdue")
len(clients) counts the items in the list (3), and range(3) counts up 0, 1, 2 — exactly the positions you need to look up the matching entry in both lists.
What you just learned
Lists store many things in order; for loops repeat one instruction per item; if inside a loop lets you filter or build a shortlist; and indexing two lists together with range(len(...)) lets you work with related data side by side. On their own, these four ideas already cover a surprising amount of real professional-services work.