Python Basics
Map, Filter, Lambda
Python offers a seamless entry into the realm of functional programming, providing a user-friendly environment to leverage its powerful features. Notable among these features are lambda functions, map, and filter, which serve as concise and effective tools for various functional programming tasks. Let’s dive into their usage and explore how these elements enhance the elegance and expressiveness of Python code.
Map
map is a built-in function used for applying a specified function to all items in an iterable (e.g., a list, tuple, or set) and returning an iterator that produces the results. The general syntax of the map function is as follows
map(function, iterable, ...)function: The function to apply to each item in the iterable.iterable: The iterable (e.g., list, tuple) whose elements will be processed by the function.
Here’s a simple example to find the square of each number in the list
# Function to square a number
def square(x):
return x ** 2
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Using map to apply the square function to each element in the list
squared_numbers = map(square, numbers)
# Converting the result to a list (as map returns an iterator)
result_list = list(squared_numbers)
print(result_list)
[1, 4, 9, 16, 25]In this example, the square function is applied to each element in the numbers list using map. The result is an iterator, which is then converted to a list for printing.
We can also use map with multiple iterables. The function should take as many arguments as there are iterables. For example:
# Function to add two numbers
def add(x, y):
return x + y
# Lists of numbers
numbers1 = [1, 2, 3, 4]
numbers2 = [5, 6, 7, 8]
# Using map to apply the add function to corresponding elements in the lists
sum_result = map(add, numbers1, numbers2)
# Converting the result to a list
sum_list = list(sum_result)
print(sum_list)
[6, 8, 10, 12]Filter
The filter() function is a built-in function used to filter elements from an iterable (like a list) based on a specified function. It returns an iterator containing the elements for which the function returns True. The syntax of the filter() function is as follows:
filter(function, iterable)function: A function that tests if each element of the iterable is true or false. If None, it simply returns the elements of the iterable that are true.
Get Srigin Sasidharan’s stories in your inbox
Join Medium for free to get updates from this writer.
iterable: An iterable like a list, tuple, or set whose elements are tested by the function.
Here’s a simple example of using filter() to filter even numbers from a list:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Define a function to check if a number is even
def is_even(x):
return x % 2 == 0
# Use filter to get a list of even numbers
even_numbers = list(filter(is_even, numbers))
print(even_numbers) # Output: [2, 4, 6, 8]In this example, the is_even function is used as the filtering criterion for the filter() function.
Lambda
a lambda function is a small anonymous function defined using the lambda keyword. Lambda functions can take any number of arguments but can only have one expression. The general syntax is as follows:
lambda arguments: expressionHere’s a simple example of a lambda function that calculates the square of a number:
square = lambda x: x**2
# Using the lambda function
result = square(5)
print(result) # Output: 25Lambda functions are often used when you need a small function for a short period and don’t want to formally define a full function using the def keyword.
Lambda functions are commonly used with functions like map, filter, and sorted. Here are a few examples:
i. Using map with a lambda function to square each element in a list:
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x**2, numbers))
print(squared_numbers) # Output: [1, 4, 9, 16, 25]ii. Using filter with a lambda function to filter even numbers:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers) # Output: [2, 4, 6, 8]iii. Using sorted with a lambda function to sort a list of tuples based on the second element:
pairs = [(1, 5), (3, 2), (7, 1), (4, 3)]
sorted_pairs = sorted(pairs, key=lambda x: x[1])
print(sorted_pairs) # Output: [(7, 1), (3, 2), (4, 3), (1, 5)]In this exploration of Python’s versatile trio — lambda, map, and filterfunctions, we've delved into the power of functional programming and concise, expressive code.
Thank you for joining me on this journey into the world of lambda, map, and filter functions in Python. As you apply these concepts to your projects, may your code be clean, your logic be sharp, and your programming endeavors be both enjoyable and rewarding.





