Get Index of Multiple List Elements in Python
Last Updated :
05 Feb, 2024
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)
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)
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)
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)
Similar Reads
Index of Non-Zero Elements in Python list We are given a list we need to find all indexes of Non-Zero elements. For example, a = [0, 3, 0, 5, 8, 0, 2] we need to return all indexes of non-zero elements so that output should be [1, 3, 4, 6].Using List ComprehensionList comprehension can be used to find the indices of non-zero elements by ite
2 min read
Multi-dimensional lists in Python There can be more than one additional dimension to lists in Python. Keeping in mind that a list can hold other lists, that basic principle can be applied over and over. Multi-dimensional lists are the lists within lists. Usually, a dictionary will be the better choice rather than a multi-dimensional
3 min read
Check if element exists in list in Python In this article, we will explore various methods to check if element exists in list in Python. The simplest way to check for the presence of an element in a list is using the in Keyword. Example:Pythona = [10, 20, 30, 40, 50] # Check if 30 exists in the list if 30 in a: print("Element exists in the
3 min read
Python List index() - Find Index of Item index() method in Python is a helpful tool when you want to find the position of a specific item in a list. It works by searching through the list from the beginning and returning the index (position) of the first occurrence of the element you're looking for. Example:Pythona = ["cat", "dog", "tiger"
3 min read
Get index in the list of objects by attribute in Python In this article, we'll look at how to find the index of an item in a list using an attribute in Python. We'll use the enumerate function to do this. The enumerate() function produces a counter that counts how many times a loop has been iterated. We don't need to import additional libraries to utili
2 min read