Access List Element by Index in Python (3 Examples)

 

In this Python article you’ll learn how to extract list elements by index.

The article contains three examples. To be more specific, the content of the tutorial looks as follows:

Let’s dive right in…

 

Creation of Example Data

We’ll use the following data as a basis for this tutorial:

my_list = ['a', 'x', 'c', 'y', 'b']         # Create example list
print(my_list)                              # Print example list
# ['a', 'x', 'c', 'y', 'b']

As you can see, we have constructed a list object that contains five elements.

Let’s extract some of these data!

 

Example 1: Extract Single List Value Based on Index Position

This example illustrates how to use square brackets to access a single element in a list.

For this, we can simply specify the index position of the value that we want to return. In this specific example, we’ll return the item at the index position 2.

print(my_list[2])                           # Extract list element by index
# c

As shown in the previous output, the character ‘c’ is located at the second index position of our list.

 

Example 2: Extract Multiple List Values Based on Index Position

It is also possible to return multiple elements from a list.

In this example, I’ll explain how to achieve this using the itemgetter function from the operator module.

Let’s first import itemgetter:

from operator import itemgetter             # Import itemgetter

Next, we can apply the itemgetter function to access several elements simultaneously:

print(itemgetter(*[2, 4])(my_list))         # Return multiple list elements by indices
# ('c', 'b')

In this specific example, we have returned the second and fourth index locations, i.e. the strings ‘c’ and ‘b’.

 

Example 3: Show Values & Indices in pandas DataFrame

Especially when dealing with larger lists, it might be difficult to find the index position of a certain element.

Example 3 explains how to create a data set that shows list values and indices side-by-side.

For this task, we first need to load the pandas library:

import pandas as pd                         # Load pandas package

In the next step, we can employ the DataFrame constructor to create a pandas DataFrame containing our list values and the corresponding indices:

df_list = pd.DataFrame({'value':my_list,    # Create DataFrame with values & indices
                        'index':range(0, len(my_list))})
print(df_list)                              # Print DataFrame

 

table 1 DataFrame access list element index python

 

Have a look at the table that got returned after running the previous code. It shows that our example data is constituted of five rows and two variables.

The first column contains our list values, and the second column shows the corresponding index positions.

Note that Python starts counting from 0. Hence, the first row corresponds to the index 0, the second row corresponds to the index 1, and so on…

 

Video & Further Resources

Have a look at the following video on my YouTube channel. In the video, I demonstrate the content of this tutorial:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Besides that, you could read some of the other tutorials on my homepage:

 

In this Python tutorial you have learned how to access list elements by their index position. Let me know in the comments section below, in case you have additional questions. Furthermore, please subscribe to my email newsletter to receive updates on the newest articles.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top