Topic 6 Python’s enumerate
Let’s suppose that I have a list:
fruits = ['apple', 'cherry', 'banana', 'mango', 'watermelon', 'tomato']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
forloopfor 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!