Access Index of List in Python (2 Examples)
In this Python tutorial you’ll learn how to access the index of an element in a Python list.
The page consists of the following topics:
So let’s get started…
The following data will be used as a basis for this Python programming tutorial.
sample_list = ["Hello", "Dear", "Programmer", "of", "the", "Globe", "Welcome", "to", "Statistics", "Globe"]
Our example list is called sample_list and contains ten character strings.
Example 1: Finding the Index of an Element in a List
In this example, I’ll demonstrate how to find the index of the element “Statistics” in the list. To achieve this, we can use the index() method of the list module.
print(sample_list.index("Statistics")) # getting and printing the index of "Statistics" # 8
Since the index values start from 0 in Python programming, the ninth element in the list has an index value of 8.
Example 2: Finding all Indices of a Repeated Element in the List
This example illustrates how to find all indices of an element in a list. If there are multiple occurrences of an element in a list, we should first use the function enumerate(). This function returns a list of tuples where each element of the list is transformed to (index, value) form.
Thus, if we use the function on the sample_list we get the following output.
for tuple in enumerate(sample_list): # for loop iterating through tuples print(tuple) # printing the tuples # (0, 'Hello') # (1, 'Dear') # (2, 'Programmer') # (3, 'of') # (4, 'the') # (5, 'Globe') # (6, 'Welcome') # (7, 'to') # (8, 'Statistics') # (9, 'Globe')
Then, we can use another loop to iterate through the entire list and store the indices of the value that we are searching for. If we wanted to search for the word “Globe”, then it would be as follows.
matching_indices = [i for i, x in enumerate(sample_list) if x == "Globe"] # finding all the indices print(matching_indices) # printing the indices # [5, 9]
Here, the for loop iterates through every tuple of the enumerated list and assigns the index to ‘i’ and the value to ‘x’ at each round. Afterwards, the value is checked if it is “Globe” and if there is a match, the indices are stored in the resulting list.
Video & Further Resources
Do you need more explanations on the examples of this tutorial? Then you might want to have a look at the following video instruction on my YouTube channel. I’m explaining the Python programming codes of this tutorial in the video:
The YouTube video will be added soon.
Furthermore, you may have a look at the other Python articles which I have published on my website.
- Check if List Index Exists in Python
- Check if Value Exists in List of Dictionaries in Python
- Using Lists in Python
- Convert pandas DataFrame Index to List & NumPy Array in Python
- Access Index of Last Element in pandas DataFrame in Python
- Python Programming Tutorials
At this point you should know how to get the index of an element in a list in Python programming. Let me know in the comments if you have further questions.
This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.
Statistics Globe Newsletter