In this post, we are going to learn how to access Multiple list elements by index python with program examples. The list is an in-built ordered collection of sequence data type in Python that is represented by square brackets([]) and each item in the list is separated by a comma(,).
1. How to Access Multiple list elements by index Python
We have a list “mylist” to access mutiple elements from this list based on indexes. We have used list comprehension with enumerate() method that an if condition that checks if given Indexes =[1, 2, 3,6,9] are exists in the original list.The Enumerate() method add a counter/index to an sequence(tuple,list,dictionary) and return as an enumerate object.The enumerate object can be directly used with for loop or list comprehension to iterate over as we have used the below program.
mylist = [3, 6, 9, 'Rust', 'Java', 'C#', 12,15,18,21,24]
Indexes =[1, 2, 3,6,9]
items = [item for x, item in enumerate(mylist) if x in Indexes]
print(items)
Output
['6', '9', 'Rust', '12', '21']
2. Access mutiple list elements by index Python
In this python program, we have used list comprehension to iterate over the list. The list. index() with if condition to accessing multiple elements from the given Indexes = [1, 2, 3,6,9].
mylist = [3, 6, 9, 'Rust', 'Java', 'C#', 12,15,18,21,24]
Indexes = [1, 2, 3,6,9]
items = [item for item in mylist if mylist.index(item) in Indexes ]
print(items)
Output
['6', '9', 'Rust', '12', '21']
3. Access multiple list elements by index using getitem()
We have used the list getitem() built-in python function that returns values of given indexes. The map() function is used to apply the getitem() function on each index in the list (Indexes) and return all the elements at given indexes and to get a list of elements we have converted into a list
mylist = [3, 6, 9, 'Rust', 'Java', 'C#', 12,15,18,21,24]
Indexes = [1, 2, 3,6,9]
items = list(map(mylist.__getitem__, Indexes))
print(items)
Output
['6', '9', 'Rust', '12', '21']
4. Numpy to Access multiple list elements by index Python
In this python program example, we have used the Python Numpy module to access the multiple elements from the list based on indexes. First, we have converted the list into a numpy array by using code np.array(mylist) .To access multiple elements from a converted numpy array we have used nparr[Indexes]
- To use a NumPy library, First, we have to install and import it into our code.
- Second, create a NumPy array from the list.
- accessing element based on indexes from numpy array
- In the final step, convert the numpy array to a list to get the result.
import numpy as np
mylist = [3, 6, 9, 'Rust', 'Java', 'C#', 12,15,18,21,24]
Indexes = [1, 2, 3,6,9]
#creating a numpy array
nparr = np.array(mylist)
#accessing element based on indexes from numpy array
lstitems = nparr[Indexes]
#converting numpy array to list
result_lst = list(lstitems)
print(result_lst)
Output
['6', '9', 'Rust', '12', '21']
5. Function to Access Mutiple list elements by index
In this python program example, we have defined a custom function accessed_Indexes_item that accepts list and indices as parameters. To access all the elements by indices we have used list compression by accessing elements from the list by their index position.
mylist = [3, 6, 9, 'Rust', 'Java', 'C#', 12,15,18,21,24]
Indexes = [1, 2, 3,6,9]
def accessed_Indexes_item(list, indexes):
result_lst = [list[indx] for indx in indexes]
return result_lst
print(accessed_Indexes_item(mylist,Indexes))
Output
['6', '9', 'Rust', '12', '21']
Summary
In this post, we have how to Access Multiple list elements by index Python and list methods index(), list.getitem() with examples.