Python filter() Function – Tutorial with Examples

The filter() function in Python is a built-in function that is used to filter elements from a sequence (such as a list, tuple, or string) based on a specified condition. It takes two arguments: a function and an iterable object. The function specifies the condition that elements must satisfy to be included in the filtered list, and the iterable object is the sequence of elements to be filtered.

Syntax

filter(function, iterable)

Parameters

  • function – The function that defines the condition to be satisfied by the elements of the iterable object. The function must return a boolean value.
  • iterable – The sequence of elements to be filtered. Can be a list, tuple, or string.

Return Value

The filter() function returns a filter object that can be converted to other Python objects like a list, tuple, set, etc. using the appropriate conversion functions, such as list(), tuple(), or set().

Examples

Example 1: Filtering Odd Numbers from a List

def is_odd(num):
  return num % 2 != 0
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
odd_numbers = list(filter(is_odd, numbers))
print(odd_numbers)

Output:

[1, 3, 5, 7, 9]

In this example, a function is_odd() is defined that takes a number as an argument and returns True if the number is odd, and False otherwise. The list of numbers is then passed to the filter() function along with the is_odd() function, and the result is stored in the variable odd_numbers. The filtered list contains only the odd numbers from the original list.

Example 2: Filtering Strings with Length Greater than 5

def is_long(string):
  return len(string) > 5
strings = ["hello", "world", "Python", "is", "awesome"]
long_strings = list(filter(is_long, strings))
print(long_strings)

Output:

['hello', 'world', 'Python']

In this example, a function is_long() is defined that takes a string as an argument and returns True if the length of the string is greater than 5, and False otherwise. The list of strings is then passed to the filter() function along with the is_long() function, and the result is stored in the variable long_strings. The filtered list contains only the strings with length greater than 5 from the original list.

Example 3: Filtering Negative Numbers from a List

def is_negative(num):
  return num < 0
numbers = [1, -2, 3, -4, 5, -6, 7, -8, 9]
negative_numbers = list(filter(is_negative, numbers))
print(negative_numbers)

Output:

[-2, -4, -6, -8]

In this example, a function is_negative() is defined that takes a number as an argument and returns True if the number is negative, and False otherwise. The list of numbers is then passed to the filter() function along with the is_negative() function, and the result is stored in the variable negative_numbers. The filtered list contains only the negative numbers from the original list.

Use Cases

The filter() function is commonly used in the following scenarios:

  • Removing unwanted elements from a sequence based on a specified condition
  • Extracting elements from a sequence that satisfy a certain condition
  • Selecting elements from a sequence that meet certain criteria

In general, the filter() function is an efficient and convenient way to process and manipulate sequences of elements in Python. It is also easily composable with other functions, making it a powerful tool for complex data processing tasks.

Leave a Reply

Your email address will not be published. Required fields are marked *