🌱 Junior Track 22 of 22

22. Functional Programming

Write side-effect-free, declarative code with Python's functional toolkit: master `map()`, `filter()`, `reduce()`, `zip()`, `enumerate()`, `any()`, and `all()` for elegant data processing pipelines.

👁️ Views: 0
# ==============================================================================
# JUNIOR PYTHON TRACK 22 — FUNCTIONAL PROGRAMMING PRIMITIVES
# ==============================================================================
from functools import reduce

# Raw Sample Data
usernames = ["alice", "bob", "charlie", "diana"]
roles = ["admin", "user", "admin", "user"]
scores = [95, 42, 88, 76]

# 1. enumerate() — Numbering tasks with 1-based index
indexed_users = list(enumerate(usernames, start=1))

# 2. zip() — Combining parallel lists into user dictionary records
user_records = [
    {"name": name, "role": role, "score": score}
    for name, role, score in zip(usernames, roles, scores)
]

# 3. filter() & lambda — Filter admin users
admins = list(filter(lambda u: u["role"] == "admin", user_records))

# 4. map() & lambda — Extract scores of admins
admin_scores = list(map(lambda u: u["score"], admins))

# 5. reduce() — Sum total admin scores
total_admin_score = reduce(lambda acc, val: acc + val, admin_scores, 0)

# 6. any() & all() — Boolean checks on datasets
has_perfect_score = any(s == 100 for s in scores)  # False
all_passed = all(s >= 40 for s in scores)            # True

if __name__ == "__main__":
    print("Indexed Users:", indexed_users)
    print("Admins:", [a["name"] for a in admins])
    print("Total Admin Score:", total_admin_score)
    print("All Students Passed (>=40):", all_passed)
🌱 Junior Track 22 Master Reference

22. Built-in Functional Primitives Cheat Sheet

📊 Built-in Functional Utilities Matrix

Function Input Output Primary Purpose
map() Function + Iterable Iterator Transforms every item element-wise.
filter() Predicate + Iterable Iterator Selects items meeting condition.
reduce() Binary Function + Sequence Single Scalar Folds sequence into accumulated result.
zip() Multiple Iterables Tuple Iterator Pairs matching elements across streams.
enumerate() Iterable + optional start (index, item) Iterator Attaches auto-incrementing count.
any() / all() Iterable of Booleans bool Short-circuit truth assertions.

⭐ Rate Track 22: Functional Programming (1 to 5 Stars)

Your rating is stored directly in the database. Average: 5.0 / 5 (1 rating)

★ ★ ★ ★ ★
← 21. Lambda Functions 🌱 Junior Hub ⭐ Practice 100+ Python Questions →