top of page

Enumerate function for lists

Writer's picture: Ekta AggarwalEkta Aggarwal

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('--')

Comments


Don't Miss Out

Sign Up and Get All Notifications

Thanks for submitting!

  • Facebook
  • LinkedIn

©2023 by Analytics is Normal

bottom of page