Last updated: Apr 10, 2024
Reading timeยท5 min
To access multiple elements in a list by their indices:
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']
The example uses a list comprehension to iterate over the collection of indices.
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.
If you use NumPy, you can access a NumPy array at multiple indices directly.
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']
Make sure you have numpy installed to be able to run the code sample.
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.
This is a two-step process:
operator.itemgetter()
class to get a callable object that fetches
the items.list()
class to convert the result to a list.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']
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]
.
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.
This is a three-step process:
for
loop to iterate over the collection of indices.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']
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.
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com']
Alternatively, you can use the map()
function.
This is a three-step process:
map()
function.list()
class to
convert the map object to a list.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 map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
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.
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.
pip install pandas pip3 install pandas
Now import pandas
and use a Series
object to access multiple elements by
their indices.
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 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.
You can learn more about the related topics by checking out the following tutorials:
:
), ellipsis (...
), numpy.newaxis (None
) and integer or boolean arrays are valid indices