+ 2

What does 'enumerate' do?

a = [1,2,3] for x,y in enumerate(a,2): print(x,y) ''' 2 1 3 2 4 3 '''

16th Apr 2019, 6:00 AM
Thimira Rathnayake
Thimira Rathnayake - avatar
2 Answers
+ 6
It adds an index to every element of your iterable. Default is 0. So in a list ['a', 'b'] you would get the elements (0, 'a') and (1, 'b'). With the second argument you can set the starting point of the indexing. This can for example be convenient when you need both index and element in a for loop. for index, element in my_list: ....
16th Apr 2019, 6:38 AM
HonFu
HonFu - avatar
+ 2
Thanks! HonFu
16th Apr 2019, 6:43 AM
Thimira Rathnayake
Thimira Rathnayake - avatar