Tutorial #12: Super QUICK way to filter a list with Python filter() function _ Python for Beginners

preview_player
Показать описание
Without using for or while loop, you can filter a Python list very quickly with filter() function. I love this method because it's convenient, just one line of code, and easy to apply. Of course you can use this function for multiple conditions with lambda expression.

Functions used in the video:
filter(function, iterable):
Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.
Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.

Lambda expressions:
Lambda expressions (sometimes called lambda forms) are used to create anonymous functions. The expression lambda parameters: expression yields a function object. The unnamed object behaves like a function object defined with:
-------------------------------------------------------------
Python Tutorial for Beginners
Python Coding for Beginners
Python Programming for Beginners
Learn Python by Building Projects
Python Exercises and Practical Examples with solutions
Practice Python Online with solution
How to write a Python program
Python Programming Data Structure and Algorithm
Python Problem Solving
Python Game Tutorial
Python Game Programming
Python Game Development
Python Tips and Tricks

Tag
Filter (extract/remove) items of a list with filter in Python,
Python filter list multiple conditions,
python lambda list filtering with multiple conditions,
How do you filter a list by condition in Python,
Python Multiple Criteria list Filtering,
Python filter list by condition,
python filter list lambda,
Python Filter Function with Lambda Expression,
how to filter data in python,
How do you filter items in a list in Python,
How do you filter a string list in Python,
python filter list by condition,
python filter list of dictionaries,
python filter list comprehension
Рекомендации по теме
Комментарии
Автор

There are 3 popular approaches to filter a list in Python:
1. Use the in-built filter() function
2. Use loop (for or while)
3. Use list comprehension
It's your choice to use what method you are more familiar with, but for me, I prefer one-line code method by filter function as shown in the video

makeeverydayezday
Автор

So far I've only used list comprehension approach to filter a list or extract list items. Thanks for sharing another good way to quickly filter a list!

timothyleary
Автор

A great explained video, very useful for me for better understanding of list filter, list comprehension and lambda function. Thank you.

andrewpotter
Автор

Superb explanation!!! I have always confused with Map / Filter / Reduce and List comprehension concepts !!! I am very much clear now exactly when and how to use them.

sammccane
Автор

How about filter by string containing "a" ?

ahowlmx
Автор

Quick notes:
List = mutable
remove, pop, insert, append, extend, join, split, max, min, sum, sorted, sort
Tuple= immutable
Sets= used as membership checker
Doesn't allow duplicate data
union, difference, intersection

danlewis