Lists are just like arrays but they can store items of different datatypes in them such as integers, floating numbers, strings etc. They can also store lists in them. For example a=[[1,2,3],[‘apple’, ‘mango’],[2.3, 1.5, 9.0, 2, ‘cherry’]]. Here ‘a’ is a list. Now, the problem is how will you search whether the given element exists in a list or not. In this tutorial, we will discuss how to search a list of lists in python to determine whether a particular element exist in a list or not.
If you want to learn more about Python Programming, visit Python Programming Tutorials.
Some of the methods to search the list of data in the lists are:
- Search a list of lists using loops
- Search a list of lists using any() function
- Search a list of lists using in operator function
Search A list of lists using loops
In this method, we use for loop to search the desired data by traversing over the whole list. For more details regarding how to implement a for loop on a list, check out this link. First of all, create and initialize a list. Then define another variable “To_find” which contain the data to be find out from the lists. We have used nested for loops in which the first loop traverses over the list items which is also a list and then the second for loop checks the item in that list as shown below.
# list of list of data to be used Data_lists = [['Apple(34Rs)', 'Banana(56Rs)'],['Orange(66RS)','Peach(22)']] # data to find out from lists To_find = 'Apple(34Rs)' data_in_lists = False for list in Data_lists: if To_find in list: data_in_lists = True print("Whole Data lists: ", Data_lists) print("Desired Data is:",data_in_lists)
Whole Data lists: [['Apple(34Rs)', 'Banana(56Rs)'], ['Orange(66RS)', 'Peach(22)']] Desired Data is: True
Search list of lists using any() function in python
To create a new list based on the values of an existing list, list comprehension offers a more concise syntax. In this method, we first search the data element in the sublists and then use Boolean “True or False” to determine whether the given item is present or not. Python any() function takes an iterable as an input and returns true if any item in an iterable is true. An iterable can be a list, tuple, dictionary or a set etc. We can use this function to search the desired data item in a list of lists. In the any(), we search element by element with sublist command to go inside the list of lists and result of the search in being printed with a Boolean “True”.
# Create and initialize a input list of lists Data_lists = [['Apple', 'Banana'],['Orange','Peach']] # data to find out from lists To_find = 'Apple' data_in_lists = False data_in_lists = any(To_find in sublist for sublist in Data_lists) print("Input Data lists: ", Data_lists) print("Desired Data is:",data_in_lists)
Input Data lists: [['Apple', 'Banana'], ['Orange', 'Peach']] Desired Data is: True
Search A list of listS using in operator function in python
Another method is to use in operator to find out the desired data item in a list. Given a list of lists, the code below shows how to search a specific data item using in operator.
# list of list of data to be used Data_lists = [['Apple', 'Banana'], ['Orange', 'Peach']] # data to find out from lists To_find = 'Cucumber' data_in_lists = False data_in_lists = To_find in (item for sublist in Data_lists for item in sublist) print("Complete Data lists: ", Data_lists) if(data_in_lists =='True'): print("Desired data item exists.") else: print("Desired data item not found.
Complete Data lists: [['Apple', 'Banana'], ['Orange', 'Peach']] Desired data item not found.
Searching a value from a list of integers, floating numbers and strings is quite easy. You can use a simple for loop along with if else statement to find a particular item. This article is about searching a particular item from a list of lists containing items of different datatypes. You can use for loop, list comprehension, any() function, in operator to find an item. There are several other methods also which you can explore on your own. For more tutorials about python programming language, contact us. Let us know about your feedback in the comments.