Find element in list Python
In this post, you will learn how to find an element in the list using the Python programming.
A list is a sequence of indexed and ordered values like an array. It is mutable, which means we can change the order of elements in a list. In the development process, we may need to find one or more elements in a list. In Python, there are different ways to find one or more elements in a list.
Find element in list by index
In the given example, we have used the Python list index() method to find an element in the list. The index() method searches for the given element in the list and returns its position. If the similar element is available more than once, this method returns the index of the first occurrence of the element. The index in Python starts from 0.
foods = ['pasta', 'burger', 'pizza', 'muffins']
index = foods.index('burger')
print('The index of burger is:', index)
Output of the above code:
The index of burger is: 1
Find elements in list by using naive method
We can find more elements in the list by using a naive method. In the given example, we are finding elements of a list by indices present in another list.
def findElements(lst1, lst2):
return [lst1[i] for i in lst2]
# Driver code
lst1 = [43, 41, 52, 61, 23, 32]
lst2 = [2, 0, 3, 4]
print(findElements(lst1, lst2))
Output of the above code:
[52, 43, 61, 23]
Find elements in list by using Python map()
Python's inbuilt map() function applies a function on all the items of an iterator given as input and returns a map object of the results after applying the given function to each item. The iterator can be a list, tuple, dictionary or string. Here, we are finding elements of a list by using the Python map() function.
def findElements(list1, list2):
return list(map(list1.__getitem__, list2))
# Driver code
list1 = [63, 21, 53, 23, 62, 26, 69]
list2 = [3, 2, 1, 4]
print(findElements(list1, list2))
Output of the above code:
[23, 53, 21, 62]
Find elements in list by using Python itemgetter()
The itemgetter() function is used to fetch items at specific indices from an iterable. This function is equivalent (but faster) to a lambda function.
from operator import itemgetter
def findElements(lst1, lst2):
return list((itemgetter(*lst2)(lst1)))
# Driver code
lst1 = [42, 53, 12, 51, 36, 28]
lst2 = [0, 3, 4]
print(findElements(lst1, lst2))
Output of the above code:
[42, 51, 36]
Related Articles
Convert Python list to numpy array
Convert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary