Sometimes we need to keep track of the number of iterations. In case of lists this can be done so using enumerate( ) function
salary = [1000,2000,3000,4000]
list(enumerate(salary))
Enumerate function zips the iteration number with each element in a list to form a tuple.

We can iterate over a list using its index by defining 2 iterators in for loop and our iterable would be enumerate(list_name)
In the following code our 2 iterators are: index, value
We are iterating upon : enumerate(salary)
for index, value in enumerate(salary):
print(index)
print(value)
print('--')

enumerate( ) provides the option to set the starting value of the index. By default start = 0.
In the following code we have defined that our indexing should start from 1.
for index, value in enumerate(salary,start = 1):
print(index)
print(value)
print('--')

Kommentare