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 element along with the index on which it is found.

Let’s see all the different way 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

 
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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

 
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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.