Use the enumerate() function

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

for i, v in enumerate(L):

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.

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

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.


my_list = ['Python', 'Java', 'PHP', 'Javascript']
//<enumerate object at 0x00000161C7FCD030>

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. Examples:

L = ['Python', 'Java', 'PHP', 'Javascript']
e = enumerate(L)
//[(0, 'Python'), (1, 'Java'), (2, 'PHP'), (3, 'Javascript')]
L1 = ['Monday', 'Wednesday', 'Friday']
e1 = enumerate(L1)
//((0, 'Monday'), (1, 'Wednesday'), (2, 'Friday'))
L2 = {1: 'one', 2:'two', 3: 'three',4: 'four'  }
e2 = enumerate(L2)
//{(0, 1), (1, 2), (2, 3), (3, 4)}

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

L = ['Python', 'Java', 'C++', 'PHP']
e = enumerate(L)
//{0: 'Python', 1: 'Java', 2: 'C++', 3: 'PHP'}

A common use case of the enumerate function is with for loops to keep the track of the current iteration. For example:

L = ['Django', 'Flask', 'Numpy', 'Pandas', 'Pytorch']
for i, v in enumerate(L):
    print('{}: {}'.format(i, v))

//0: Django
//1: Flask
//2: Numpy
//3: Pandas
//4: Pytorch