Topic 6 Python’s enumerate
Let’s suppose that I have a list:
= ['apple', 'cherry', 'banana', 'mango', 'watermelon', 'tomato'] fruits
Now, imagine that I wanted to iterate through fruits
and print out each fruit. I’m sure you know that I can iterate through fruits
in two ways:
Using a
for
loopfor fruit in fruits: print(fruit)
Using indices
for i in range(len(fruits)): print(fruits[i])
However, there is also another way: using enumerate()
- a function that is not mentioned in your lectures or tutorials!