Filter, Map function in Python
- Get link
- X
- Other Apps
Python provides two built-in functions, filter()
and map()
, which are used to manipulate lists, tuples, and other iterable objects. These functions allow us to perform operations on each element of the iterable, without having to write explicit loops. In this blog post, we will discuss the filter()
and map()
functions and how they can be used in Python.
Filter Function
The filter()
function in Python is used to filter out elements from an iterable based on a given condition. It takes two arguments: a function and an iterable. The function passed to filter()
should take a single argument and return a boolean value. If the function returns True
, the element is included in the filtered result, and if it returns False
, the element is excluded.
Here is an example of how to use the filter()
function in Python:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] def is_even(n): return n % 2 == 0 even_numbers = list(filter(is_even, numbers)) print(even_numbers)
In this example, we define a function called is_even()
that returns True
if a given number is even, and False
otherwise. We then use the filter()
function to filter out all the even numbers from the numbers
list, and store them in a new list called even_numbers
. Finally, we print out the even_numbers
list.
Map Function
The map()
function in Python is used to apply a given function to each element of an iterable and return a new iterable with the results. It takes two arguments: a function and an iterable. The function passed to map()
should take a single argument and return a transformed value.
Here is an example of how to use the map()
function in Python:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] def square(n): return n * n squared_numbers = list(map(square, numbers)) print(squared_numbers)
In this example, we define a function called square()
that takes a number and returns its square. We then use the map()
function to apply the square()
function to each element of the numbers
list and store the results in a new list called squared_numbers
. Finally, we print out the squared_numbers
list.
Combining Filter and Map
We can also combine the filter()
and map()
functions to filter and transform elements of an iterable at the same time. Here is an example:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] def square_if_even(n): if n % 2 == 0: return n * n else: return None squared_even_numbers = list(filter(lambda x: x is not None, map(square_if_even, numbers))) print(squared_even_numbers)
square_if_even()
that returns the square of a number only if it is even. If the number is odd, it returns None
. We then use the map()
function to apply square_if_even()
to each element of the numbers
list and use the filter()
function to remove any None
values from the resulting list. Finally, we store the results in a new list called squared_even_numbers
and print it out.filter()
and map()
functions in Python are powerful tools that allow us to manipulate lists, tuples, and other iterable objects with ease. With filter()
, we can filter out elements from an iterable based on a given condition, and with map()
, we can apply a given function to each element of an iterable and return a new iterable with the results. These functions can also be combined to filter and transform elements of an iterable at the same time. By using these functions, we can write more concise and readable code that is easy to understand and maintain.- Get link
- X
- Other Apps
Comments