GeeksforGeeks App
Open App
Browser
Continue

# Python | Accessing index and value in list

There are various methods to access the elements of a list, but sometimes we may require to access an element along with the index on which it is found. Let’s see all the different ways of accessing both index and value in a list.

Method #1: Naive method This is the most generic method that can be possibly employed to perform this task of accessing the index along with the value of the list elements. This is done using a loop.

## Python3

 # Python3 code to demonstrate# to get index and value# using naive method # initializing listtest_list = [1, 4, 5, 6, 7] # Printing listprint("Original list is : " + str(test_list)) # using naive method to# get index and valueprint("List index-value are : ")for i in range(len(test_list)):    print(i, end=" ")    print(test_list[i])

Output

Original list is : [1, 4, 5, 6, 7]
List index-value are :
0 1
1 4
2 5
3 6
4 7

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #2: Using list comprehension This method works in similar way as the above method but uses the list comprehension technique for the same, this reduces the possible lines of code to be written and hence saves time.

## Python3

 # Python3 code to demonstrate# to get index and value# using list comprehension # initializing listtest_list = [1, 4, 5, 6, 7] # Printing listprint("Original list is : " + str(test_list)) # using list comprehension to# get index and valueprint("List index-value are : ")print([list((i, test_list[i])) for i in range(len(test_list))])

Output

Original list is : [1, 4, 5, 6, 7]
List index-value are :
[[0, 1], [1, 4], [2, 5], [3, 6], [4, 7]]

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #3: Using enumerate() This is the most elegant method to perform this particular problem and is highly recommended to be used in case we require to get the index along with the value in the list. This method enumerates for index along with its value.

## Python3

 # Python3 code to demonstrate# to get index and value# using enumerate # initializing listtest_list = [1, 4, 5, 6, 7] # Printing listprint("Original list is : " + str(test_list)) # using enumerate to# get index and valueprint("List index-value are : ")for index, value in enumerate(test_list):    print(index, value)

Output

Original list is : [1, 4, 5, 6, 7]
List index-value are :
0 1
1 4
2 5
3 6
4 7

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #4: Using zip() Another method that is basically used to bind the index with the corresponding value, zip() can also be possibly used to get index along with its value.

## Python3

 # Python3 code to demonstrate# to get index and value# using zip() # initializing listtest_list = [1, 4, 5, 6, 7] # Printing listprint("Original list is : " + str(test_list)) # using zip() to# get index and valueprint("List index-value are : ")for index, value in zip(range(len(test_list)), test_list):    print(index, value)

Output

Original list is : [1, 4, 5, 6, 7]
List index-value are :
0 1
1 4
2 5
3 6
4 7

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #5: Using numpy.ndenumerate()

1. Import the numpy module.
2. Initialize the list.
3. Convert the list into a numpy array.
4. Iterate over the numpy array using the numpy.ndenumerate() function.
5. Print the index and value of each element.

## Python3

 import numpy as np # initializing listtest_list = [1, 4, 5, 6, 7]  # Printing listprint("Original list is : " + str(test_list)) # using numpy.ndenumerate() to get index and valueprint("List index-value are : ")for index, value in np.ndenumerate(test_list):    print(index[0], value)

Output:

Original list is : [1, 4, 5, 6, 7]
List index-value are :
0 1
1 4
2 5
3 6
4 7

Time Complexity: O(n), where n is the length of the list.

Space Complexity: O(n), as we are creating a numpy array of size n.

Method #6: Using heapq :

1. Import the heapq module.
2. Initialize the list test_list with some values.
3. Print out the original list test_list.
4. Create an empty list heap to store the index and value pairs in a heap.
5. Iterate over the list test_list using enumerate.
6. For each index and value pair, use heapq.heappush() to add the pair to the heap list.
7. heappush() maintains the heap property so that the smallest element in the heap is always at the root.
8. Enter a while loop that runs until there are no more elements in the heap.
9. Inside the loop, use heapq.heappop() to retrieve the smallest element from the heap.
10. heappop() removes the element from the heap and returns it as a tuple containing the index and value of an element in the original list.
11. Print out the index and value of the element.

## Python3

 import heapq # initializing listtest_list = [1, 4, 5, 6, 7] # Printing listprint("Original list is : " + str(test_list)) # using heapq to get index and valueprint("List index-value are : ")heap = []for index, value in enumerate(test_list):    heapq.heappush(heap, (index, value)) while heap:    index, value = heapq.heappop(heap)    print(index, value)#This code is contributed by Vinay pinjala.

Output

Original list is : [1, 4, 5, 6, 7]
List index-value are :
0 1
1 4
2 5
3 6
4 7

Time complexity :

Initializing the list takes O(N) time, where N is the length of the list.
The for loop iterates over each element in the list and uses heapq.heappush() to add it to the heap, which takes O(log N) time for each push. Therefore, the loop takes O(N log N) time.
The while loop runs until the heap is empty, which takes O(N) iterations at most.
Inside the while loop, heapq.heappop() takes O(log N) time to retrieve the smallest element from the heap and remove it.
Printing the index and value takes O(1) time.
Therefore, the overall time complexity of the algorithm is O(N log N) due to the heapq.heappush() operation.

Space complexity :

The list test_list takes O(N) space to store the original list.
The heap list takes O(N) space to store the index and value pairs in the heap.
The index and value variables used inside the loop take O(1) space.
Therefore, the overall space complexity of the algorithm is O(N).

My Personal Notes arrow_drop_up