enumerate()
enumerate() is a built in Python function that adds a
counter to any iterable automatically. Instead of
manually creating and incrementing a counter variable yourself,
enumerate() handles it for you and returns both the
index and the item on each iteration.
It is not as commonly used as range() but when you need
to know the position of each item as you loop through a collection,
enumerate() is the cleanest and most Pythonic way to do it.
The structure looks like this:
for i, item in enumerate(iterable):
Just like iterating over a dictionary with .items(), we
unpack two values on each iteration. i holds the current
index and item holds the current value. The index starts
at 0 by default but you can change the starting value
by passing a second argument to enumerate().
enumerate() with a String
Each character in the string is returned along with its position. Try changing the string and observe how the index updates automatically.
enumerate() with a List
This is the most common use of enumerate(). Instead of
using range(len(list)) to track the index, you can wrap
the list directly.
enumerate() with a Tuple
Works exactly the same as a list. Each item is returned with its index.
enumerate() with a Dictionary
When used with a dictionary, enumerate() returns the
index and the key by default. If you need the values
as well you can combine it with .items().
Changing the Start Index
By default enumerate() starts counting from
0. You can change this by passing a start value as the
second argument. This is useful when you want to display a numbered
list to a user starting from 1 instead of 0.
Loading quiz...
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named enumerate.py in your part47 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1: Basic enumerate()
-
Task: Create a list
countries = ['Thailand', 'Japan', 'Brazil', 'Italy', 'Egypt']. -
Goal: Use
enumerate()to loop through the list and print each country with its index in the following format:0 Thailand. Then modify the loop to start the index at 1 instead of 0 and write a comment explaining how you changed the starting value.
Exercise 2: Find the Index
-
Task: Create a list using
list(range())of your choice and choose a number that exists in that range. -
Goal: Use
enumerate()to loop through the list and find the index of your chosen number. Print the result in the following format:the index of 7 is: 7. Write a comment explaining why the index and the value might match in some cases and when they would not.
Exercise 3: enumerate() with a String
-
Task: Create a variable
sentence = "enumerate". -
Goal: Use
enumerate()to loop through the string and print each character and its index. Then write a second loop that prints only the characters at even indexes. Write a comment explaining how you used the index to filter the output.
Exercise 4: enumerate() with a Condition
-
Task: Create a list
scores = [45, 78, 92, 55, 88, 34, 76, 91, 60, 49]. -
Goal: Use
enumerate()to loop through the list and print the index and score of every student who scored above 75 in the following format:student 2 scored 92. Write a comment explaining how the index and the condition work together.
Exercise 5: enumerate() with a Dictionary
-
Task: Create the following dictionary:
profile = {'name': 'Flat_Eric', 'age': 27, 'language': 'Python', 'is_student': True}. -
Goal: Use
enumerate()combined with.items()to loop through the dictionary and print the index, key and value of each entry in the following format:0 name Flat_Eric. Write a comment explaining what combiningenumerate()with.items()gives you that neither one alone would provide.
Don't Forget to commit and Push!