+ 2
why use enumerate?
what is the advantage of using "for x in enumerate(my_list)" over "for x in my_list"?
2 Answers
+ 1
That is because enumerate(my_list) iterates through the list and it's index respectively producing (index, list_item) pairs. But for loop produces only the list items.
example:
nums = [4, 6, 8, 10]
for x in enumerate(nums):
print(x)
This produces: x = (0, 4), (1, 6), (2, 8), (3, 10)
0
Because, you'll have different output.