Understand the Concept of Filtering
00:00 Welcome to this lesson, where you’ll dive into the fascinating world of filtering iterables. As a reminder, an iterable is any object that can be looped over, such as a list, a string, and a generator. Now let’s clarify what filtering is.
Then you loop through each number in the input list using a
for loop For each number, you check if it’s greater than
0 or not using an
if statement. If it is, you append it to the
When you run the
extract_positive() function with the
numbers list as its input, you get
2 as a result. So
0 have been filtered out since their filtering condition evaluated as
To summarize what you’ve learned so far, a filtering condition is a statement that evaluates to either
False based on a given criterion. For example, the
if statement from before,
if number > 0, is a filtering condition.
0 got disqualified in the example since their filtering condition evaluated as
False. Congratulations, you now know the essence of filtering. In the next lesson, you’ll explore how to use the
filter() function in Python to filter iterables.
Become a Member to join the conversation.