Get Index of Multiple List Elements in Python Last Updated : 05 Feb, 2024 Comments Improve Suggest changes Like Article Like 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 PythonBelow, are the ways To Get Index Of Multiple List Elements In Python. Using Naive Method Using a For LoopUsing List ComprehensionUsing filter() FunctionGet Index Of Multiple List Elements Using Naive MethodIn 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 LoopIn 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 ComprehensionIn 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() FunctionIn 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] Comment More infoAdvertise with us Next Article Get Index of Multiple List Elements in Python ayushkhan Follow Improve Article Tags : Python Geeks Premier League Geeks Premier League 2023 Practice Tags : python 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 Python | Indices of Kth element value Sometimes, while working with records, we might have a problem in which we need to find all the indices of elements for a particular value at a particular Kth position of tuple. This seems to be a peculiar problem but while working with many keys in records, we encounter this problem. Let's discuss 4 min read Access List Items in Python Accessing elements of a list is a common operation and can be done using different techniques. Below, we explore these methods in order of efficiency and their use cases. Indexing is the simplest and most direct way to access specific items in a list. Every item in a list has an index starting from 2 min read Get a list as input from user in Python We often encounter a situation when we need to take a number/string as input from the user. In this article, we will see how to take a list as input from the user using Python.Get list as input Using split() MethodThe input() function can be combined with split() to accept multiple elements in a sin 3 min read Python List of Lists A list of lists in Python is a collection where each item is another list, allowing for multi-dimensional data storage. We access elements using two indices: one for the outer list and one for the inner list. In this article, we will explain the concept of Lists of Lists in Python, including various 3 min read Taking multiple inputs from user in Python While taking a single input from a user is straightforward using the input() function, many real world scenarios require the user to provide multiple pieces of data at once. This article will explore various ways to take multiple inputs from the user in Python.Using input() and split()One of the sim 5 min read Like