Map, filter, and reduce are built-in functions in Python that operate on iterable objects like lists, tuples, or sets. Map applies a function to each element, filter selects elements based on a condition, and reduce performs a cumulative operation on the elements.


#Map, Filter, Reduce
my_pets = ['alfred', 'tabitha', 'william', 'arla']
uppered_pets = []

for pet in my_pets:
    pet_ = pet.upper()
    uppered_pets.append(pet_)

print(uppered_pets)

#Alternative UPPERCASE method
my_pets = ['alfred', 'tabitha', 'william', 'arla']
uppered_pets = list(map(str.upper, my_pets))

print(uppered_pets)

#Multiple Arguments Example
circle_areas = [3.56773, 5.57668, 4.00914, 56.24241, 9.01344, 32.00013]
result = list(map(round, circle_areas, range(1, 7)))
print(result)

Map allows us to apply a function to each element of an iterable, creating a new iterable with the transformed values. It simplifies the process of performing repetitive operations or calculations on multiple data points.


#(Map) Functions Expanded
my_strings = ['a', 'b', 'c', 'd', 'e']
my_numbers = [1, 2, 3, 4, 5]
results = list(zip(my_strings, my_numbers))

print(results)

#Filter Functions >75
scores = [66, 90, 68, 59, 76, 60, 88, 74, 81, 65]
def is_A_student(score):
    return score > 75

over_75 = list(filter(is_A_student, scores))

print(over_75)

#Filter Palindorme Detector
dromes = ("demigod", "rewire", "madam", "freer", "anutforajaroftuna", "kiosk")
palindromes = list(filter(lambda word: word == word[::-1], dromes))

print(palindromes)

Filter enables us to selectively extract elements from an iterable based on a given condition. It returns a new iterable containing only the elements that satisfy the specified criterion, improving data filtering and selection tasks.


#Reduce Functions
from functools import reduce
numbers = [3, 4, 6, 9, 34, 12]
def custom_sum(first, second):
    return first + second

result = reduce(custom_sum, numbers)
print(result)

#Reduce Initial Value
from functools import reduce

numbers = [3, 4, 6, 9, 34, 12]

def custom_sum(first, second):
    return first + second

result = reduce(custom_sum, numbers, 10)
print(result)

Reduce applies a cumulative function to a sequence of elements, resulting in a single value. It is useful for performing operations like summation, product calculation, or finding the maximum or minimum value.

Map, Filter, Reduce Exercise Solution


#Code Completed
from functools import reduce 

my_floats = [4.35, 6.09, 3.25, 9.77, 2.16, 8.88, 4.59]
my_names = ["olumide", "akinremi", "josiah", "temidayo", "omoseun"]
my_numbers = [4, 6, 9, 23, 5]

map_result = list(map(lambda x: round(x ** 2, 3), my_floats))
filter_result = list(filter(lambda name: len(name) <= 7, my_names))
reduce_result = reduce(lambda num1, num2: num1 * num2, my_numbers)

print(map_result)
print(filter_result)
print(reduce_result)

Map, filter, and reduce functions in Python provide powerful tools for streamlining data transformations. By leveraging these functions, we can easily apply functions to each element, filter data based on specific criteria, and reduce sequences into single values.