why use enumerate? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

why use enumerate?

what is the advantage of using "for x in enumerate(my_list)" over "for x in my_list"?

21st Jul 2016, 7:39 PM
Michael Horne
Michael Horne - avatar
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)
22nd Jul 2016, 7:42 AM
Benneth Yankey
Benneth Yankey - avatar
0
Because, you'll have different output.
21st Jul 2016, 11:46 PM
Владимир Соловьев
Владимир Соловьев - avatar