Get First Index of List in Python (3 Examples)
The content of the tutorial is structured as follows:
Let’s go straight to the code!
Create Example Data
As the first step, we will create a sample string list named
my_list to use in the following examples.
my_list = ['a', 'b', 'c', 'd', 'e'] print(my_list) # ['a', 'b', 'c', 'd', 'e']
Let’s see how to retrieve the first index in our list!
Example 1: Get First Element of List Using Indices
Before getting into how to get the first index in
my_list, it might be interesting to know how to get the first element in this list. The first index in
my_list can be found as follows.
first_element = my_list print(first_element) # a
As you can see,
my_list contains the first element in our list. Now we know that the first element in
my_list is ‘a’, we can use this information to return the index number at this position as well.
In our simplified example, we know that this index number is 0. However, sometimes we might not know the index number that corresponds to a certain element. In these cases, the following code might be useful, so keep on reading!
Example 2: Get First Index of List Using index() Function
We can get the index of the first element in our list using the index() function.
first_index = my_list.index('a') print(first_index) # 0
As you can see,
my_list.index('a') returns the index of the first element ‘a’ in
my_list, which is 0.
Example 3: Get First Index of List Using for Loop
Using a for loop is another way to get the index of the first element in
my_list. Let’s take a look!
for i in range(len(my_list)): if my_list[i] == 'a': first_index = i break print(first_index) # 0
In this example, the for loop iterates over each index in the list using the range of the length of the list, and checks if the element at that index is equal to ‘a’. When it finds ‘a’, it assigns the index to
first_index and breaks out of the loop.
Example 4: Get First Index of List Using List Comprehension
Using list comprehension can also be handy if you want to print the index of the first element.
first_index = [i for i in range(len(my_list)) if my_list[i] == 'a'] print(first_index) # 0
This code finds the first occurrence of ‘a’ in
my_list and prints its index. If there is no ‘a’ in the list, it will raise an IndexError since the list comprehension will be an empty list, and it’s trying to access the first element of it.
Video, Further Resources & Summary
Do you need more explanations on how to get the first index in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you may want to have a look at some other articles on Statistics Globe:
- Get Next Element in List in Python
- Get Index of Item in 2D List in Python
- Find Index of List Element Conditionally in Python
- Print Shortest & Longest String in List in Python
- Python Overview
This post has shown how to get the first index of a list in Python. If you have further questions, please let me know in the comments section.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get further information about her academic background and the other articles she has written for Statistics Globe.
Statistics Globe Newsletter