5 Ways of Filtering Python Lists – KDnuggets

Filter the list elements using for loop, list comprehension, regex, filter(), and lambda functions.

5 Ways of Filtering Python Lists
Image by Author

 
In this quick tutorial, you will learn 5 simple ways to filter the list elements. It is not limited to data folks, even web developers and software engineers use it on a daily basis. In short, filtering the list is the basic functionality that every Python programmer should learn at the beginning. 
 
 
By using the for loop and if-else statements, we will iterate over the list and select the elements that fit the specific condition. In our case, we will filter out the profits that are greater than or equal to 100. 
By using this method, we are creating a new list and adding the filtered value to the new list. It is a simple, but not efficient way of filtering the list.  
 
 
 
List comprehension is a smart way of using for-loop and if-else conditions on lists. You can translate method one into a single line of code. It is clean.
In our case, we are running the loop over all of the list elements and selecting the score that is greater than or equal to 150. 
It is easy to write, and you can even add multiple if-else conditions without an issue. 
Learn list comprehension with code examples by reading When to Use a List Comprehension in Python
 
 
 
To filter the string list, we will use `re.match()`. It requires the string pattern and the string. 
In our case, we are using lists comprehension to filter out the names that start with “N” by providing a regex pattern of “N.*” to `re.match()`. 
You can learn, build, and test regex patterns by visiting regex101.
 
 
 
The `filter()` is a built-in Python function to filter list items. It requires a filter function and list `filter(fn, list)`.
In our case, we will create a filter_height function. It returns True when height is less than 150 else False
After that, we will apply the filter_height function to the list using the `filter()` function, and in return, we get an iterator of filtered elements. You can either use a loop to extract all of the elements or convert it into a list using the `list(<iter>)` function. 
 
 
 
You can convert method four (filter() Method) into a single line by using lambda function. 
Instead of creating a filter_age function separately, we can write the condition within the `filter()` function using lambda. 
In our case, we are filtering the age that is greater than 50 and converting the filtered iterator into the list. 
Learn more about lambda functions by following the Python Lambda tutorial.
 
 
 
Abid Ali Awan (@1abidaliawan) is a certified data scientist professional who loves building machine learning models. Currently, he is focusing on content creation and writing technical blogs on machine learning and data science technologies. Abid holds a Master’s degree in Technology Management and a bachelor’s degree in Telecommunication Engineering. His vision is to build an AI product using a graph neural network for students struggling with mental illness.
 
Get the FREE ebook ‘The Great Big Natural Language Processing Primer’ and the leading newsletter on AI, Data Science, and Machine Learning, straight to your inbox.
By subscribing you accept KDnuggets Privacy Policy
Get the FREE ebook ‘The Great Big Natural Language Processing Primer’ and the leading newsletter on AI, Data Science, and Machine Learning, straight to your inbox.
By subscribing you accept KDnuggets Privacy Policy
Subscribe To Our Newsletter (Get The Great Big NLP Primer ebook)
Get the FREE ebook ‘The Great Big Natural Language Processing Primer’ and the leading newsletter on AI, Data Science, and Machine Learning, straight to your inbox.
By subscribing you accept KDnuggets Privacy Policy
Get the FREE ebook ‘The Great Big Natural Language Processing Primer’ and the leading newsletter on AI, Data Science, and Machine Learning, straight to your inbox.
By subscribing you accept KDnuggets Privacy Policy

source

Leave a Comment