lambda functions, enumeratem, zip, map, filter
Here’s an easy-to-understand guide to lambda, functions, enumerate, zip, map, filter in Python 🐍✨—with emojis, explanations, code examples, and practice questions!
🔹 lambda (Anonymous Functions)
- lambda creates small, unnamed functions on the fly.
- Syntax:
lambda arguments: expression
- Useful for short, simple functions, especially as arguments to other functions.
# Example: square of a number
square = lambda x: x * x
print(square(5)) # 25
# Example with two arguments
add = lambda x, y: x + y
print(add(3, 4)) # 7
You can assign a lambda to a variable or use it directly!
🔹 Functions
- Defined using
def
. - Can have parameters and return values.
def greet(name):
return f"Hello, {name}!"
print(greet("Alice")) # Hello, Alice!
Functions organize code and make it reusable.
🔹 enumerate
- Adds a counter to an iterable (like a list or string).
- Returns pairs of (index, item).
fruits = ['apple', 'banana', 'cherry']
for idx, fruit in enumerate(fruits):
print(idx, fruit)
# Output:
# 0 apple
# 1 banana
# 2 cherry
Great for when you need both the item and its position! 12
🔹 zip
- Combines multiple iterables into tuples, stopping at the shortest.
- Useful for parallel iteration.
names = ['Anil', 'Bina', 'Chetan']
marks = [85, 92, 78]
for name, mark in zip(names, marks):
print(name, mark)
# Output:
# Anil 85
# Bina 92
# Chetan 78
You can use list(zip(...))
to see all pairs at once.
12
🔹 map
- Applies a function to every item in an iterable.
- Returns an iterator (use
list()
to get all results).
nums = [1, 2, 3, 4]
squares = list(map(lambda x: x*x, nums))
print(squares) # [1, 4, 9, 16]
map is often used with lambda for concise code! 12
🔹 filter
- Filters items in an iterable by a function that returns True/False.
- Returns an iterator (use
list()
to get all results).
nums = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens) # [2, 4, 6]
filter is great for extracting items that meet a condition! 12
🏋️♂️ Practice Questions
1️⃣ Use map
to add 10 to every number in [^1][^2][^3]
.
Solution
nums = [1, 2, 3]
result = list(map(lambda x: x + 10, nums))
print(result) # [11, 12, 13]
2️⃣ Use filter
to keep only words longer than 3 letters in ['cat', 'lion', 'tiger']
.
Solution
words = ['cat', 'lion', 'tiger']
result = list(filter(lambda w: len(w) > 3, words))
print(result) # ['lion', 'tiger']
3️⃣ Use enumerate
to print the index and value of each character in “dog”.
Solution
for idx, char in enumerate("dog"):
print(idx, char)
# Output:
# 0 d
# 1 o
# 2 g
4️⃣ Use zip
to pair elements from [^1][^2][^3]
and ['a', 'b', 'c']
.
Solution
nums = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = list(zip(nums, letters))
print(pairs) # [(1, 'a'), (2, 'b'), (3, 'c')]
⭐ Key Points Table
Tool | What it does | Example use |
---|---|---|
lambda | Creates small, quick functions | lambda x: x+1 |
enumerate | Adds index to items | for i, v in enumerate(list) |
zip | Pairs elements from iterables | zip(list1, list2) |
map | Applies function to all items | map(lambda x: x*2, nums) |
filter | Keeps items meeting a condition | filter(lambda x: x>0, nums) |
Practice these tools—they make your Python code powerful and concise! 🚀 If you want more examples or have questions, just ask!
⁂