In this article, we will instruct you to Get access to the index in the map()
function in Python by using the enumerate()
function. Moreover, we also show you how to create a list by list comprehension, which is faster than using the map()
function.
The enumerate() function
enumerate()
is a built-in function in Python that takes an iterable object, such as a list, tuple, or string, and returns an iterator that produces tuples containing the index and the original element. It allows you to loop over the elements of the iterable and have access to both the element and its index within the loop. Read our previous article to see the usage of the function with the list.
Get access to the index in the map() function in Python by the enumerate() function
Here is an example of using the enumerate()
function to get access to the index in the map()
function. Read carefully.
Code:
def multiplyByTwo(x, i): return x * 2, i numbers = [1, 2, 3, 4] # Use map() and enumerate() to apply the multiply_by_two function to each element and get the index of each element result = map(lambda x, i: multiplyByTwo(x, i), numbers, enumerate(numbers)) print(list(result))
Result:
[(2, (0, 1)), (4, (1, 2)), (6, (2, 3)), (8, (3, 4))]
In this example, the enumerate()
function returns an iterator that contains pairs of the form (index, element) for each element in the numbers list. The lambda function passed to map()
receives both the element and its index as arguments and applies the multiplyByTwo() function to them. The result is a new iterator with the transformed elements and their indices.
The function returns two values x*2 and i. x is the number after being multiplied by 2 (2, 4, 6, 8), and i is the result of the enumerate()
function. Actually, i is a pair of indexes and values of elements in the list ( for example, the pair (0, 1)).
Extensive knowledge
Instead of using the map()
and lambda()
functions, you can create a new list by list comprehension, which is faster. Look at the example below; we use list comprehension to create a list that has the same values as the list in part above. Moreover, we also use the zip()
function to zip two objects: the list of numbers and the enumerate()
function to be able to run 2 loops at the same time.
Code:
def multiplyByTwo(x, i): return x * 2, i numbers = [1, 2, 3, 4] # Use list comprehension, use the zip() function to run 2 loop parallelly result = list(multiplyByTwo(x, i) for x, i in zip(numbers, enumerate(numbers))) print(list(result))
Result:
[(2, (0, 1)), (4, (1, 2)), (6, (2, 3)), (8, (3, 4))]
Summary
In summary, this article shows you how to access the index in the map()
function by using the enumerate()
function. We also display the advantage of list comprehension when creating a list and remind you about enumerate()
and zip()
functions.

My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.
Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP