Filter List in PythonWhile using shopping apps, suppose we want to buy a T-shirt; we type it in the search bar, and hundreds of results appear. We'll have some criteria in our mind on what we want to choose like the amount of money we want to spend on the T-shirt, colour etc. Hence, shopping apps provide a 'filter' option using which we can only view the results that already satisfy our pre-defined criteria. In the same way, while working on huge amounts of data, we'll sometimes have to filter the data based on some criteria. For example, In the 3 billion population of India, if we want information about only people of a certain age group, we can filter the 3 billion profiles based on age group. This tutorial discusses how to filter the data stored in a list based on the need in the program with examples. First, a list is a built-in data structure in Python in which we can store data of different types. Example 1:Suppose we have a list with marks of 60 students in a particular subject, and we want to filter out the marks less than 20. So here, the criterion is to eliminate all the values less than 20. Code: Output [30, 40, 56, 54, 34] Example 2:Suppose we have some characters in a list, and we want to filter all the consonants out of the list. Output ['a', 'e', 'i', 'o'] In both the above examples, we created a new list and appended the elements from the original list that satisfied the criteria we wanted. Another Approach:We can also create a copy of the original list and delete the elements that do not satisfy the criteria. Example: Output ['a', 'e', 'i', 'o']
Python's built-in Function:These are the general approaches, but Python is a language that makes everything simpler. It provides a built-in function called filter() that filters the lists based on the criterion we give. Syntax: criterion: The testing standard we provide to filter the list. The function checks if every sequence element returns True when tested with the criterion. sequence: The data structure we provide can be a list, tuple, set or any other iterable data type.
Let us take the two examples we used earlier and see how filter() works: Output
Example: Output ['a', 'e', 'i', 'o'] Lambda Function + filter():Creating a function every time we need the filter() can be tiring, mostly if we won't need it any other time. Hence, we mostly use lambda functions with the function. If you don't know about lambda functions, here is a small promo: A lambda function is a function without a name-an anonymous function. It can take any number of arguments and permits a single expression. We use the lambda function to create a function needed for a short time without much long-term significance. Syntax: Example: Output 5 Here are the same two examples we used earlier made easier using the lambda functions: Output [30, 40, 56, 54, 34] Output ['a', 'e', 'i', 'o'] Filtering the List of Lists:Suppose we are doing an investigation and we have a list of possible criminals. We know that the age of the accused is between 20 and 30. Now, we need to filter the lists: Output [['Roberto', 23], ['Ross', 21], ['Chandler', 23], ['Joey', 27]] Next TopicPYTHON PROJECTS IN NETWORKING |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India