-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap_Filter_Reduce.py
More file actions
25 lines (25 loc) · 1.53 KB
/
Map_Filter_Reduce.py
File metadata and controls
25 lines (25 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# map()
#Purpose: Applies a function to each item in a sequence and returns a new sequence with the transformed items.
#Inputs: A function + an iterable (like a list).
#Output: A new iterable (like a list) where each element is the result of the function applied to the original element.
numbers = [1, 2, 3, 4]
squares = list(map(lambda x: x**2, numbers))
print(squares) # Output: [1, 4, 9, 16]
#---------------------------------------------------------------------------------------------------------------------------
# filter()
#Purpose: Filters elements from a sequence using a condition.
#Inputs: A function that returns True or False + an iterable.
#Output: A new iterable containing only the elements that satisfy the condition.
numbers = [1, 2, 3, 4]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens) # Output: [2, 4]
#---------------------------------------------------------------------------------------------------------------------------
# reduce()
#Purpose: Reduces a sequence to a single value by repeatedly applying a function to pairs of elements.
#Inputs: A function + an iterable.
#Output: A single result, like a total or product.
from functools import reduce
numbers = [1, 2, 3, 4]
total = reduce(lambda x, y: x + y, numbers)
print(total) # Output: 10
#---------------------------------------------------------------------------------------------------------------------------