ExampleEdit & Run

Use the enumerate() function

L = ['Python', 'Django', 'Flask', 'Numpy', 'Pandas']

for i, v in enumerate(L):
    print(i,v)
copy
Output:
0 Python 1 Django 2 Flask 3 Numpy 4 Pandas [Finished in 0.011287039145827293s]

The enumerate() function allows you to iterate through a sequence of objects while keeping track of the index of each item.The function takes one required arguments, an iterable  and an optional argument, the start. The iterable is the object containing the elements to enumerate while the start indicates the starting index , it defaults to 0.

Syntax:
enumerate(iterable, start = 0)
copy
iterable Required argument representing the iterable to enumerate.
start An optional integer specifying the starting index. It defaults to 0.
Parameters:

The function returns an iterable enumerate object containing  index-value pairs, where the value is an element from the iterable argument and the index is its index as specified by the start argument.

ExampleEdit & Run
my_list = ['Python', 'Java', 'PHP', 'Javascript']

result = enumerate(my_list)

print( result )
copy
Output:
<enumerate object at 0x7f6b9b410180> [Finished in 0.010693829972296953s]

We can turn the enumerate object to other iterable types such as lists, tuples and set  using the relevant constructors, in this case, the resulting object will contain a tuple containing the index-value objects.

ExampleEdit & Run
L = ['Python', 'Java', 'PHP', 'Javascript']
e = enumerate( L )
print( list(e) )

L1 = ['Monday', 'Wednesday', 'Friday']
e1 = enumerate( L1 )
print( tuple(e1) )

L2 = {1: 'one', 2:'two', 3: 'three',4: 'four'  }
e2 = enumerate( L2 )
print( set(e2) )
copy
Output:
[(0, 'Python'), (1, 'Java'), (2, 'PHP'), (3, 'Javascript')] ((0, 'Monday'), (1, 'Wednesday'), (2, 'Friday')) {(0, 1), (1, 2), (2, 3), (3, 4)} [Finished in 0.010330112185329199s]

When the enumerate object is used with the dict function, it will result to a dictionary containing the indices as the keys and the values as the dictionary values.

ExampleEdit & Run
L = ['Python', 'Java', 'C++', 'PHP']

e = enumerate(L)

print( dict(e) )
copy
Output:
{0: 'Python', 1: 'Java', 2: 'C++', 3: 'PHP'} [Finished in 0.009938962291926146s]

A common use case of the enumerate() function is with for loops to keep the track of the index of the current iteration element.

ExampleEdit & Run
L = ['Django', 'Flask', 'Numpy', 'Pandas', 'Pytorch']

for i, v in enumerate(L):
    print( '{}: {}'.format(i, v) )
copy
Output:
0: Django 1: Flask 2: Numpy 3: Pandas 4: Pytorch [Finished in 0.010220412164926529s]

Note that using the enumerate() as in above is more convenient than the alternative approaches. For example, without using enumerate() we would need to use other means to track the current index as shown below.

ExampleEdit & Run
L = ['Django', 'Flask', 'Numpy', 'Pandas', 'Pytorch']

for i in range( len(L) ):
    print( '{}: {}'.format( i, L[i] ) )
copy
Output:
0: Django 1: Flask 2: Numpy 3: Pandas 4: Pytorch [Finished in 0.010116050019860268s]

enumerate() with start

The start parameter allows us to specify an integer as the starting value for the enumeration.  By default, this value is 0.

ExampleEdit & Run
L = ['Django', 'Flask', 'Numpy', 'Pandas', 'Pytorch']

result = enumerate(L, start = 10)

print( list(result) ) 
copy
Output:
[(10, 'Django'), (11, 'Flask'), (12, 'Numpy'), (13, 'Pandas'), (14, 'Pytorch')] [Finished in 0.010008186101913452s]

The start value can also be negative as shown below.

ExampleEdit & Run
L = ['Django', 'Flask', 'Numpy', 'Pandas', 'Pytorch']

result = enumerate(L, start = -6)

print( list(result) ) 
copy
Output:
[(-6, 'Django'), (-5, 'Flask'), (-4, 'Numpy'), (-3, 'Pandas'), (-2, 'Pytorch')] [Finished in 0.009669410064816475s]