Python Tutorial: Filter Function 🎯

beginner
19 min

Python Tutorial: Filter Function 🎯

Welcome to the Filter Function lesson! In this tutorial, we'll dive into one of Python's powerful built-in functions that helps you filter out elements from a list or other iterable objects based on a specified condition. Let's get started! 🚀

What is the Filter Function? 📝

The filter() function in Python is used to filter out elements from a given iterable (like lists, tuples, or sets) based on a specified condition. The filtered elements are returned as a new list.

Syntax 💡

The filter() function takes two arguments:

  1. A function that returns True or False for each element in the iterable.
  2. The iterable that you want to filter.
python
filter(function, iterable)

How it works 💡

Let's understand this with a simple example. Suppose we have a list of numbers and we want to filter out only the even numbers.

python
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] def is_even(num): # This function checks if the number is even if num % 2 == 0: return True else: return False # Using the filter function to filter even numbers even_numbers = filter(is_even, numbers) # Convert the filter object to a list even_numbers_list = list(even_numbers) print(even_numbers_list)

Output:

[2, 4, 6, 8]

In this example, the is_even() function checks if the number is even (returns True) or odd (returns False). The filter() function applies this condition to the numbers list, and only the even numbers are returned.

Practical Applications 💡

The filter() function is incredibly useful in real-world projects and data analysis. It can help filter users based on specific criteria in a database, filter out errors in a list, or filter out specific elements in a complex data structure.

Advanced Example 💡

Let's consider a more advanced example where we filter out words with lengths greater than 5 from a list of words.

python
words = ['apple', 'banana', 'cherry', 'orange', 'watermelon', 'grape'] def word_length(word): # This function checks the length of a word return len(word) > 5 # Using the filter function to filter words with length greater than 5 long_words = filter(word_length, words) # Convert the filter object to a list long_words_list = list(long_words) print(long_words_list)

Output:

['watermelon']

In this example, the word_length() function checks the length of a word. The filter() function applies this condition to the words list, and only the word with a length greater than 5 is returned.

Quiz Time 🎯

Quick Quiz
Question 1 of 1

What does the `filter()` function do in Python?

Wrapping Up ✅

In this lesson, we learned about the powerful filter() function in Python, which helps us filter out elements from a given iterable based on a specified condition. We also saw practical examples and an advanced example, and took a quick quiz to reinforce our understanding.

In the next lesson, we'll explore another important built-in Python function: the map() function!

Stay tuned and happy coding! 💻💖