Lesson 5 of 21 · 52 min · UG / professional
Looping techniques: index, zip, dict keys
Sometimes you need the seat number and the student. Sometimes two lists must walk in lockstep (marks and rolls). enumerate and zip are the laptop names; you will write the loops by hand first.
Weather station
Filter a table of readings.
You will be able to
- ✓ Walk a list with a running index (enumerate on a laptop)
- ✓ Walk two lists in lockstep by the same index (zip on a laptop)
- ✓ Loop the keys of a small record when you must print every field
Sometimes you need the seat number and the student
Hall seating is not only names. The invigilator wants seat 0 Ira, seat 1 Kabir. On a laptop, enumerate(names) hands you the index and the value together. Here you keep a counter: i starts at 0, print, then i = i + 1. That is the whole trick. Do not invent a second list of seats unless the seats are not 0..n-1.
Marks and rolls must walk together: roll 12 scored 41. zip(rolls, marks) is the laptop name. Here the lists are parallel — same length, same order — and you index one while you walk the other, or you walk with a while and i = i + 1. If the lengths differ, that is a data error: print a status, do not guess.
Dict keys are a list you can name
CONFIG has college, gst, pf. Looping keys on a laptop is for k in CONFIG. This sandbox is happiest if you keep an explicit list of keys and read CONFIG[k]. The idea is the same: visit every labelled field without copying the print line three times.
Do not zip two dicts and hope the insertion order matches a clerk’s spreadsheet. Zip sequences you already sorted into a policy. Keys of a record are a contract; parallel lists are a pairing.
Laptop: enumerate and zip
names = ["Ira", "Kabir"]
for i, name in enumerate(names):
print(i, name)
rolls = [12, 7]
marks = [41, 32]
for roll, mark in zip(rolls, marks):
print(roll, mark)
Words that matter
- enumerate
- Laptop helper: index plus value as you walk a sequence.
- zip
- Laptop helper: walk two (or more) sequences in lockstep.
- Parallel lists
- Same length, same order — one fact split across two names.
Common mistakes
Avoid: Zipping marks to rolls when one list is longer and silently dropping the tail.
Do this: Check len; then walk; refuse if the pairing is incomplete.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — seat numbers by hand
Counter starts at 0. Second print is seat 1.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — rolls zip marks
Index 0 is roll 12 with mark 41.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Example program — Print every CONFIG field
Keys listed once. Values follow.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Label seat 1
names is Ira then Kabir. Print a line that includes both Seat 1 and Kabir.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.