Python Functional Tools ๐Ÿง  | Dive into map, filter, zip, etc. #13 || Python ka Pitara || DeathCode - DeathCode

Built-in Functions Mastery (map, filter, zip, enumerate, any, all)

๐ŸŒŸ Introduction

"Python me aise kuch tools hote hain jo chhote dikhte hai, lekin kaam karte hain bade logon wale!

Aaj hum dekhenge 6 aise built-in functions jo har Python coder ko master karne chahiye."

Covered:

  • map()
  • filter()
  • zip()
  • enumerate()
  • any()
  • all()

1๏ธโƒฃ map() โ€” The Transformer

nums = [1, 2, 3, 4]
squares = list(map(lambda x: x ** 2, nums))
# Output: [1, 4, 9, 16]

"Map function har item pe function apply karta hai. Clean, fast, aur memory-friendly!"

Real Use:

  • String to float
  • Discount logic
  • Normalize data

Tips:

  • Avoid if lambda gets too complex
  • Prefer def in such case

2๏ธโƒฃ filter() โ€” The Bouncer

nums = [1, 2, 3, 4, 5]
evens = list(filter(lambda x: x % 2 == 0, nums))
# Output: [2, 4]

"Filter unhi values ko rakhta hai jo condition me true return karti hai."

Combine with map:

squared_evens = list(map(lambda x: x**2, filter(lambda x: x%2==0, nums)))

Use:

  • Remove blank entries
  • Clean junk data

3๏ธโƒฃ zip() โ€” The Zipper

names = ["A", "B", "C"]
marks = [90, 80, 70]
combined = list(zip(names, marks))
# [('A', 90), ('B', 80), ('C', 70)]
  • Combines element-wise
  • Shortest list wins

Unzip:

a, b = zip(*combined)

Use:

  • Join columns
  • Merge CSV

Tips:

  • Use zip with *args to reverse!

4๏ธโƒฃ enumerate() โ€” Index Manager

fruits = ["apple", "banana", "cherry"]
for idx, fruit in enumerate(fruits):
    print(f"{idx}: {fruit}")

Bonus:

enumerate(fruits, start=1)

Use:

  • Index + item print
  • Replace range(len(...)) loop

5๏ธโƒฃ any() & all() โ€” Truth Checkers

any([False, False, True])  # True
all([True, True, False])   # False

Real Use:

  • Form validation
  • Checking if any user active

List Comp:

ages = [21, 34, 18]
all_adult = all([age >= 18 for age in ages])

Tip:

  • Use any() with not for cleaner logic!

๐Ÿ”ฅ Combine Like a Pro

students = ["ram", "shyam", "geeta"]
marks = [90, 45, 60]
result = list(filter(lambda x: x[1] >= 50, zip(students, marks)))
# [('ram', 90), ('geeta', 60)]

"Combination of zip + filter = superpower for tabular data!"


โš  Pitfalls & Best Practices

  • Avoid complex logic in lambda
  • Use list comprehension for readability
  • Prefer map/filter with pure functions

๐Ÿงช Practice Section

  1. map() โ†’ Uppercase list of strings
  2. filter() โ†’ Keep only prime numbers
  3. zip() โ†’ Combine 3 lists: names, emails, phones
  4. enumerate() โ†’ Index of vowels in string
  5. any() โ†’ Check if any student failed
  6. all() โ†’ All passwords โ‰ฅ 8 chars?

๐Ÿš€ WOW nice going!

"Ab aap Python ke built-in tools ke ninja ban gaye ho!

Inhe har codebase me use karo โ€” clean, fast, elegant Python!"

Agla part: Python Modules & pip Mastery ๐Ÿ”ฅ | Install, Create, Freeze & Upgrade Like a Pro! Don't miss it!

ยฉ 2024 DeathCode. All Rights Reserved.