Ojasa Mirai

Ojasa Mirai

Python

Loading...

Learning Level

🟢 Beginner🔵 Advanced
Why Functions?Parameters & ArgumentsReturn StatementsScopeDefault ParametersVariable Arguments (*args)Lambda FunctionsDecoratorsFunctional ProgrammingBest Practices
Python/Functions/Functional Programming

🎯 Functional Programming — map, filter, reduce

Process lists of data using functions instead of loops. It's cleaner and often easier to understand!


🎯 map() — Change Every Item

The `map()` function takes a function and applies it to every single item in a list, creating a new list with the transformed values. Instead of writing a loop that goes through each item manually, `map()` does that work for you. This is cleaner, more readable, and often faster than traditional loops.

numbers = [1, 2, 3, 4, 5]

# Double each number
doubled = list(map(lambda x: x * 2, numbers))
print(doubled)  # [2, 4, 6, 8, 10]

💡 Real Example

# Convert all temperatures
celsius = [0, 10, 20, 30]
fahrenheit = list(map(lambda c: (c * 9/5) + 32, celsius))
print(fahrenheit)  # [32.0, 50.0, 68.0, 86.0]

# Extract names from list of people
people = [
    {"name": "Alice", "age": 25},
    {"name": "Bob", "age": 30},
]
names = list(map(lambda p: p["name"], people))
print(names)  # ['Alice', 'Bob']

🔍 filter() — Keep Only What You Need

The `filter()` function runs a test on every item in a list and keeps only the items where the test returns `True`. This is your clean, functional alternative to writing if-statements inside loops. It's especially useful when you need to clean data, remove invalid entries, or extract only certain elements from a collection.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Keep only even numbers
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)  # [2, 4, 6, 8, 10]

💡 Real Example

# Keep only people over 18
people = [
    {"name": "Alice", "age": 25},
    {"name": "Bob", "age": 17},
    {"name": "Charlie", "age": 30},
]
adults = list(filter(lambda p: p["age"] >= 18, people))
# [{'name': 'Alice', 'age': 25}, {'name': 'Charlie', 'age': 30}]

# Keep only non-empty strings
words = ["python", "", "coding", "", "rocks"]
filtered = list(filter(None, words))  # None filters out empty strings
print(filtered)  # ['python', 'coding', 'rocks']

📈 reduce() — Combine Into One Value

The `reduce()` function takes a list of items and combines them into a single value by repeatedly applying a function. It starts with the first two items, applies your function, then uses that result with the third item, and so on. This is perfect for operations like summing, multiplying, or finding the maximum element in a list.

from functools import reduce

numbers = [1, 2, 3, 4, 5]
total = reduce(lambda a, b: a + b, numbers)
print(total)  # 15 (1+2+3+4+5)

💡 Real Examples

from functools import reduce

# Multiply all numbers
numbers = [2, 3, 4, 5]
product = reduce(lambda a, b: a * b, numbers)
print(product)  # 120

# Find maximum
numbers = [5, 2, 8, 1, 9, 3]
maximum = reduce(lambda a, b: a if a > b else b, numbers)
print(maximum)  # 9

🎨 Combining Them Together

from functools import reduce

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Get even numbers, double them, sum them
result = reduce(
    lambda a, b: a + b,
    map(lambda x: x * 2, filter(lambda x: x % 2 == 0, numbers))
)
# filter: [2, 4, 6, 8, 10]
# map: [4, 8, 12, 16, 20]
# reduce: 60
print(result)  # 60

✅ Summary Table

FunctionDoesExample
map()Transform each itemDouble all numbers
filter()Keep matching itemsKeep only even numbers
reduce()Combine all itemsSum all numbers

🔑 Key Takeaways

ConceptRemember
map()Apply function to every item
filter()Keep only matching items
reduce()Combine items into one value
Chain themPowerful when combined

🔗 What's Next?

Now let's learn best practices for writing great functions!

Next: Best Practices →


Ready to practice? Try challenges or view solutions


Resources

Python Docs

Ojasa Mirai

Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.

Learn Deep • Build Real • Verify Skills • Launch Forward

Courses

PythonFastapiReactJSCloud

© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn