Python provides built-in functions like zip(), filter(), lambda, and map() to work efficiently with lists and other iterables. They help combine data, select elements based on conditions, create small anonymous functions and apply operations to each element without writing explicit loops.
Zip
The zip() function in Python is used to combine multiple iterables such as lists or tuples into a single iterator of tuples. Each tuple contains elements taken from the iterables at the same index position. The iteration stops when the shortest iterable is exhausted.
Syntax
zip(iterable1, iterable2, ...)
Example 1: This example shows how zip() function combines two lists by pairing their elements based on index positions.
a = [1, 2, 3]
b = ['a', 'b', 'c']
res = zip(a, b)
print(list(res))
Output
[(1, 'a'), (2, 'b'), (3, 'c')]
Example 2: This example demonstrates how zip() can be used to transpose a matrix, converting rows into columns.
- *matrix: unpacks each row of the matrix.
- zip(*matrix): groups elements column-wise.
- list(row): converts each zipped tuple into a list.
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print([list(row) for row in zip(*matrix)])
Output
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Lambda
A lambda function is a small anonymous function used for simple operations without defining a full function using def. It can take any number of arguments but contains only a single expression.
Syntax
lambda arguments: expression
Example 1: This example shows how a lambda function can be used to add two numbers.
- lambda x, y: x + y defines an anonymous function that adds two values.
add = lambda x, y: x + y
print(add(5, 3))
Output
8
Example 2: This example demonstrates how a lambda function is used with filter() to select even numbers from a list.
- lambda x: x % 2 == 0: checks whether a number is even.
- filter(lambda x: ..., num): applies the condition to each element.
num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list(filter(lambda x: x % 2 == 0, num)))
Output
[2, 4, 6, 8]
Filter
The filter() function is used to select elements from an iterable that satisfy a specific condition. It applies a function to each element and returns only those for which the function returns True.
Syntax
filter(function, iterable)
Example: This example demonstrates how filter() is used to extract only even numbers from a list using a lambda expression.
- lambda x: x % 2 == 0: checks if a number is even
- filter(lambda x: x % 2 == 0, numbers): applies the condition to each element
- Only elements returning True are selected.
num = [1, 2, 3, 4, 5, 6]
res = filter(lambda x: x % 2 == 0, num)
print(list(res))
Output
[2, 4, 6]
Map
The map() function applies a given function to each element of an iterable and returns a map object (an iterator). It is used to transform data without explicit loops.
Syntax
map(function, iterable)
Example 1: This example demonstrates how to square each number in a list using a regular function.
- map(square, numbers) applies square to each element in numbers
def square(num):
return num ** 2
a = [1, 2, 3, 4]
res = map(square, a)
print(list(res))
Output
[1, 4, 9, 16]
Example 2: This example shows how a lambda function can be used with map() to calculate the length of each string in a list.
- lambda x: len(x): anonymous function that returns the length of a string
- map(lambda x: ..., names): applies the lambda to each element in the list
names = ['Jake', 'Ben', 'Harry', 'Olf']
print(list(map(lambda x: len(x), names)))
Output
[4, 3, 5, 3]