Loading...

Python enumerate and zip

for walked a row it already had. It did not put a count beside each value, and it did not walk two rows together. enumerate does the first of those. zip does the second. The number from enumerate starts at 0 unless you pass another start. zip stops when the shorter list ends. Neither word is break or continue. The loop is not being cut. It is being given a number, or a pair.

names holds lida, then guest. enumerate(names) yields a number and the name, once per item. The first number is 0, because nothing told it to start anywhere else. print writes the number and the name on the same line.

Number each name from 0

python
names = ["lida", "guest"]
for number, name in enumerate(names):
    print(number, name)

The screen shows 0 lida, then 1 guest. The 0 and the 1 are not stored inside names. names is still two strings. The number lives only for that pass.

Start the count at 1

Pass 1 as the second argument and the first number is 1. enumerate(names, 1) writes 1 lida, then 2 guest. The names do not move. Only the count moves. Leave the second argument off, and you are back at 0.

zip takes two lists and hands you one item from each, in the same order. names is lida then guest. drinks is water then tea. The first pass is lida with water. The second pass is guest with tea. There is no number unless you also use enumerate, and this program does not.

Pair each name with a drink

python
names = ["lida", "guest"]
drinks = ["water", "tea"]
for name, drink in zip(names, drinks):
    print(name, drink)

You see lida water, then guest tea. The pairing follows list order. It does not look up a name. Swap the drinks and the pairs swap with them.

The shorter list ends it

Give drinks only water. zip still starts at the front of both lists, so the first pass is lida with water. There is no second drink. zip does not invent one, and it does not raise an error. guest is simply never visited. The loop ends.

Stop when the drinks run out

python
names = ["lida", "guest"]
drinks = ["water"]
for name, drink in zip(names, drinks):
    print(name, drink)

The only line is lida water. guest is still in names. It had no drink to stand beside, so this loop did not reach it.

Four calls, and what each one writes. The two enumerate rows differ only in the start. The two zip rows differ only in the length of drinks.

📊 What each call writes

Call What you see
enumerate(names) 0 with lida, then 1 with guest
enumerate(names, 1) 1 with lida, then 2 with guest
zip(names, drinks) with water then tea lida with water, then guest with tea
zip(names, drinks) with only water lida with water, then the loop ends

The next page is about functions: a named piece of work you can call. It does not number a list, and it does not pair two lists.

Before you run it

  1. names is lida, then guest. enumerate(names) writes which lines?
  2. You pass 1 as the start. What is the first line?
  3. drinks is water, then tea. What does zip write second?
  4. drinks holds only water. Is guest written? Does the program stop with an error?

Where the pairs go wrong

  • Expecting enumerate to start at 1 when you passed no start
  • Thinking the number is stored back into the list
  • Pairing lida with tea because tea feels like the match
  • Expecting guest to appear when drinks has only one item
  • Treating the early stop as a crash

One reading

enumerate walks one list and adds a number. With no start, that number is 0, then 1: 0 with lida, then 1 with guest. Pass 1 and the same names come out as 1 and 2.
zip walks two lists in lockstep. Equal lengths give lida with water, then guest with tea. A drinks list that holds only water writes lida water and then ends. guest stays in names, unvisited.
The next page names a piece of work you can call.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top