Open In App

Get Index of Multiple List Elements in Python

Last Updated : 05 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, retrieving the indices of specific elements in a list is a common task that programmers often encounter. There are several methods to achieve this, each with its own advantages and use cases. In this article, we will explore some different approaches to get the index of multiple list elements in Python.

Get Index Of Multiple List Elements In Python

Below, are the ways To Get Index Of Multiple List Elements In Python.

Get Index Of Multiple List Elements Using Naive Method

In this example, below function `get_indices` takes a list of `lst` and a list of `targets`, returning the indices of the targets found in the original list. In the provided example, it prints the indices of ‘apple’ and 2.

Python3




def get_indices(lst, targets):
    indices = []
    for target in targets:
        if target in lst:
            indices.append(lst.index(target))
    return indices
 
# Example usage:
my_list = [1, 'apple', 3, 'banana', 2, 'orange']
target = ['apple', 2]
result = get_indices(my_list, target)
print(result)


Output

[1, 4]

Get Index Of Multiple List Elements Using For Loop

In this example, in code the`get_indices` function utilizes the `enumerate` function to iterate through elements and their indices in the list `lst`. It returns the indices of elements present in the `targets` list. In the given example, it prints the indices of ‘apple’ and 2 .

Python3




def get_indices(lst, targets):
    indices = []
    for index, element in enumerate(lst):
        if element in targets:
            indices.append(index)
    return indices
 
# Example usage:
my_list = [1, 'apple', 3, 'banana', 2, 'orange']
target = ['apple', 2]
result = get_indices(my_list, target)
print(result)


Output

[1, 4]

Get Index Of Multiple List Elements Using List Comprehension

In this example, in below code the `get_indices` function utilizes a list comprehension to directly generate a list of indices for elements in the `targets` list found in the original list `lst`. In the provided example, it prints the indices of ‘apple’ and 2.

Python3




def get_indices(lst, targets):
    return [index for index, element in enumerate(lst) if element in targets]
 
# Example usage:
my_list = [1, 'apple', 3, 'banana', 2, 'orange']
target = ['apple', 2]
result = get_indices(my_list, target)
print(result)


Output

[1, 4]

Get Index Of Multiple List Elements Using filter() Function

In this example, in below code The `get_indices` function employs the `filter` function with a lambda expression to create a list of indices for elements in the `targets` list found in the original list `lst`. In the provided example, it prints the indices of ‘apple’ and 2 .

Python3




def get_indices(lst, targets):
    return list(filter(lambda x: lst[x] in targets, range(len(lst))))
 
# Example usage:
my_list = [1, 'apple', 3, 'banana', 2, 'orange']
target = ['apple', 2]
result = get_indices(my_list, target)
print(result)


Output

[1, 4]



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads