Python Mastery – From Basics to Brilliance / Functions

zip(), map(), filter(), and reduce() functions

These built-in functions are extremely useful for concise data processing in Python.

✅ zip()

  • Combines multiple iterables (like lists) element-wise into tuples.

names = ["Alice", "Bob"]
scores = [85, 92]
combined = list(zip(names, scores))
print(combined)  # [('Alice', 85), ('Bob', 92)]

✅ map()

  • Applies a function to every item in an iterable.

def square(x):
    return x * x

numbers = [1, 2, 3]
squares = list(map(square, numbers))
print(squares)  # [1, 4, 9]

✅ filter()

  • Filters elements based on a condition.

def is_even(n):
    return n % 2 == 0

nums = [1, 2, 3, 4]
evens = list(filter(is_even, nums))
print(evens)  # [2, 4]

✅ reduce() (from functools)

  • Applies a function cumulatively to reduce a sequence to a single value.

from functools import reduce

nums = [1, 2, 3, 4]
sum_all = reduce(lambda a, b: a + b, nums)
print(sum_all)  # 10

⚡ Great for pipelines, transformations, and functional programming-style data flows.