Remember
enumerate() yields index/value pairs. The optional start changes the first index, not the items.
enumerate() yields index/value pairs. The optional start changes the first index, not the items.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
items = ["tea", "milk"]
for index, item in enumerate(items, start=3):
print(index, item)Top-level scope
['tea', 'milk']Object 4 · int4Object 5 · str'milk'enumerate() yields index/value pairs. The optional start changes the first index, not the items.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
items = ["tea", "milk"]
for index, item in enumerate(items, start=3):
print(index, item)
|
v
DISPLAYED RESULT
3 tea
4 milk
FINAL BINDINGS
items --> object 1 (list)
index --> object 4 (int)
item --> object 5 (str)
object 1: ['tea', 'milk']
object 4: 4
object 5: 'milk'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
items = ["tea", "milk"]
for index, item in enumerate(items, start=3):
print(index, item)Top-level scope
['tea', 'milk']Object 4 · int4Object 5 · str'milk'The example has finished. Compare the final output with the values and references above.
3 tea 4 milk
What do you expect this program to print?
items = ["tea", "milk"]
for index, item in enumerate(items, start=3):
print(index, item)enumerate() FunctionThe enumerate() function in Python is used to loop through a sequence (like a list, tuple, or string) while keeping track of the index of the current item.
It's a clean and Pythonic way to write loops when you need both index and value.
enumerate(iterable, start=0)
iterable – A sequence like a list, tuple, or stringstart – The index to start counting from (default is 0)(index, value) pairsfruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(index, fruit)
0 apple
1 banana
2 cherry
for index, fruit in enumerate(fruits, start=1):
print(index, fruit)
1 apple
2 banana
3 cherry
Instead of writing:
i = 0
for item in fruits:
print(i, item)
i += 1
You can simplify it using enumerate():
for i, item in enumerate(fruits):
print(i, item)
for i, char in enumerate("hello"):
print(f"Index {i} has character '{char}'")
Index 0 has character 'h'
Index 1 has character 'e'
Index 2 has character 'l'
Index 3 has character 'l'
Index 4 has character 'o'
index and value from enumerate(). Always use: for i, val in enumerate(...)enumerate() is often preferred in interviews over manually tracking counters in loops. It shows clean, Pythonic style.
enumerate() helps track both index and item while looping.start argument.Write a program that takes a list of names and prints them numbered from 1:
names = ["Alice", "Bob", "Charlie"]
for i, name in enumerate(names, start=1):
print(f"{i}. {name}")
1. Alice
2. Bob
3. Charlie
Connect what you noticed in the example with the idea below.
enumerate() yields index/value pairs. The optional start changes the first index, not the items.
The start argument skips that many items.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.