Python · Some intermediate topics
enumerate()
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — What is enumerate()?
When you loop through a list with `for`, you get each element — but sometimes you also need its position (index). You could create a counter variable yourself...
team = ["Aquachu", "Pyrochu", "Florachu"]
i = 0
for codemon in team:
print(i, codemon)
i += 1
Or do it the other way around with `range(len())`...
team = ["Aquachu", "Pyrochu", "Florachu"]
for i in range(len(team)):
print(i, team[i])
Both works, but Python has something better: `enumerate()`.
Exercise 2 — Using enumerate()
`enumerate()` wraps your list and gives you both the index and the value at the same time.
team = ["Aquachu", "Pyrochu", "Florachu"]
for index, codemon in enumerate(team):
print(index, codemon)
Exercise 3 — How enumerate() Works
`enumerate()` actually creates pairs (called tuples). Each pair contains the index and the element. Let's see what it looks like.
team = ["Aquachu", "Pyrochu", "Florachu"]
pairs = list(enumerate(team))
print(pairs)
for pair in pairs:
print(pair)
No Need to Convert
You don't need to convert to a list to use `enumerate()`. In a `for` loop, Python unpacks each pair automatically.
Exercise 4 — enumerate() Output
moves = ["Splash", "Fireball", "Leaf Cut"]
for i, move in enumerate(moves):
print(i, move)
What is the first line printed?
Options
• 1 Splash
• 0 Splash
• Splash 0
• 0, Splash
Hint : In Python, counting starts at 0, not 1.
Exercise 5 — Complete the enumerate Loop
Complete the code to display each Codémon with its position.
team = ["Zap", "Metachu", "Rockachu"]
for ____, name in ____(team):
print(i, name)
Options: i enumerate index range len
Hint : `enumerate()` gives you both the index and the value.
Exercise 6 — Custom Start Index
By default, `enumerate()` starts counting at 0. But you can change this by passing a second argument.
team = ["Aquachu", "Pyrochu", "Florachu"]
for num, codemon in enumerate(team, 1):
print(f"{num}: {codemon}")
The start Parameter
The syntax is `enumerate(list, start)`. The second argument sets where counting begins.
Exercise 7 — enumerate with start
items = ["Potion", "Shield"]
for i, item in enumerate(items, 5):
print(i, item)
Options
• 5 Potion
6 Shield
• 0 Potion
1 Shield
• 1 Potion
2 Shield
• 5 Potion
5 Shield
Hint : The start argument changes where counting begins, then it increments by 1 each time.
Exercise 8 — Start from 1
Complete the code to number the moves starting from 1.
moves = ["Splash", "Fireball"]
for num, move in enumerate(moves, ____):
print(f"Move {____}: {move}")
Options: 0 1 num move 2
Hint : The second argument to `enumerate()` sets the starting number. The variable before the colon holds the current count.
Exercise 9 — Enumerate a String
`enumerate()` works on any iterable, not just lists! Complete the code to display each character with its position.
word = "Pyrochu"
for ____, char in enumerate(____):
print(i, char)
Options: i word char enumerate 0
Hint : Strings are iterable — you can loop through their characters.
Exercise 10 — What Does enumerate Return?
What type of object does `enumerate()` produce at each iteration?
Options
• A list
• A tuple (index, value)
• Just the index
• A dictionary
Hint : Remember what we saw when we did `(enumerate(team))`.
Exercise 11 — enumerate() Expressions
Given `team = ["A", "B", "C"]`, match each expression with its result.
Items to match
• `enumerate(team)`
• `enumerate(team, 1)`
• `enumerate(team, 10)`
• `list(enumerate(team))`
Definitions
• Starts counting at 0
• Starts counting at 1
• Starts counting at 10
• [(0, 'A'), (1, 'B'), (2, 'C')]
Exercise 12 — Codémon Team Roster
Display your Codémon team as a numbered roster starting from 1.
For each Codémon, print: `"#1: Aquachu (Water)"``
Starter code
team = [("Aquachu", "Water"), ("Pyrochu", "Fire"), ("Florachu", "Plant"), ("Zap", "Electric")]
# Display the roster starting from #1
Back to the course