Access multiple elements in List by their indices in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
5 min

banner

# Table of Contents

  1. Access multiple elements in List by their indices in Python
  2. Access multiple elements in List by their indices using NumPy
  3. Access multiple elements in List by their indices using itemgetter(
  4. Access multiple elements in List by their indices using for loop
  5. Access multiple elements in List by their indices using map()
  6. Access multiple elements in List by their indices using pandas

# Access multiple elements in List by their indices in Python

To access multiple elements in a list by their indices:

  1. Use a list comprehension to iterate over the collection of indices.
  2. Access the list at the current index and return the result.
  3. The new list will only contain the items at the specified indices.
main.py
a_list = ['bobby', 'hadz', 'com', 'a', 'b', 'c'] indices = [0, 2, 4] # โœ… using a list comprehension new_list = [a_list[index] for index in indices] print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'com', 'b']

access multiple elements in list by their indices

The code for this article is available on GitHub

The example uses a list comprehension to iterate over the collection of indices.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
main.py
a_list = ['bobby', 'hadz', 'com', 'a', 'b', 'c'] indices = [0, 2, 4] new_list = [a_list[index] for index in indices] print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'com', 'b']

On each iteration, we access the list item at the current index and return the result.

The new list only contains the items at the specified indices.

# Access multiple elements in List by their indices using NumPy

If you use NumPy, you can access a NumPy array at multiple indices directly.

main.py
import numpy as np arr = np.array(['bobby', 'hadz', 'com', 'a', 'b', 'c']) indices = [0, 2, 4] new_list = list(arr[indices]) print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'com', 'b']

access multiple elements in list by indexes using numpy

The code for this article is available on GitHub

Make sure you have numpy installed to be able to run the code sample.

shell
pip install numpy pip3 install numpy

We used the numpy.array method to create a NumPy array and accessed it directly at multiple indices.

You can use a list of indices to access a NumPy array at multiple indices.

Alternatively, you can use the operator.itemgetter() class.

# Access multiple elements in List by their indices using itemgetter()

This is a two-step process:

  1. Use the operator.itemgetter() class to get a callable object that fetches the items.
  2. Use the list() class to convert the result to a list.
main.py
from operator import itemgetter a_list = ['bobby', 'hadz', 'com', 'a', 'b', 'c'] indices = [0, 2, 4] new_list = list(itemgetter(*indices)(a_list)) print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'com', 'b']

access multiple elements in list by their indices using itemgetter

The code for this article is available on GitHub

The operator.itemgetter() class returns a callable object that fetches the items at the specified indices.

For example, x = itemgetter(1) and then calling x(my_list), returns my_list[1].

Similarly, x = itemgetter(0, 2, 4) and then calling x(my_list) returns (my_list[0], my_list[2], my_list[4]).

When multiple indices are specified, the itemgetter() class returns a tuple containing the items at the specified indices.

The last step is to use the list() class to convert the tuple to a list object.

Alternatively, you can use a simple for loop.

# Access multiple elements in List by their indices using for loop

This is a three-step process:

  1. Declare a new variable that stores an empty list.
  2. Use a for loop to iterate over the collection of indices.
  3. Append the items at the specified indices to the new list.
main.py
a_list = ['bobby', 'hadz', 'com', 'a', 'b', 'c'] indices = [0, 2, 4] new_list = [] for index in indices: new_list.append(a_list[index]) print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'com', 'b']

access multiple elements in list by their indices using for loop

The code for this article is available on GitHub

We used a for loop to iterate over the collection of indices.

On each iteration, we access the list at the current index and append the item to a new list.

The list.append() method adds an item to the end of the list.

main.py
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

Alternatively, you can use the map() function.

# Access multiple elements in List by their indices using map()

This is a three-step process:

  1. Pass a lambda function and the collection of indices to the map() function.
  2. The lambda function should access the list at the given index.
  3. Use the list() class to convert the map object to a list.
main.py
a_list = ['bobby', 'hadz', 'com', 'a', 'b', 'c'] indices = [0, 2, 4] new_list = list( map(lambda index: a_list[index], indices) ) print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'com', 'b']
The code for this article is available on GitHub

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

The lambda function we passed to map() gets called with each index in the indices list and returns the item of the original list at the given index.

The last step is to use the list() class to convert the map object to a list.

Which approach you pick is a matter of personal preference. I'd use a list comprehension because I find them quite direct and easy to read.

# Access multiple elements in List by their indices using pandas

You can also use the pandas module to access multiple elements in a list by their indices.

Make sure you have pandas installed to be able to run the code sample.

shell
pip install pandas pip3 install pandas

Now import pandas and use a Series object to access multiple elements by their indices.

main.py
import pandas as pd series = pd.Series(['bobby', 'hadz', 'com', 'a', 'b', 'c']) indices = [0, 2, 4] new_list = list(series[indices]) print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'com', 'b']
The code for this article is available on GitHub

The Series class is used to store a one-dimensional ndarray and can directly be accessed using a list of indices.

The last step is to use the list() class to convert the Series object to a list.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev