Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 10 of 10 · Functions and Lambda Expressions
Interview question

What are map, filter, and reduce? map, filter, reduce क्या हैं?

Answer
from functools import reduce

# map - apply function to each element
nums = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, nums))
# [1, 4, 9, 16, 25]

# filter - select matching elements
evens = list(filter(lambda x: x % 2 == 0, nums))
# [2, 4]

# reduce - accumulate values
product = reduce(lambda x, y: x * y, nums)
# 120
map()     # हर element पर function apply करता है
filter()  # condition match करने वाले select करता है
reduce()  # values को accumulate करता है

Was this answer clear?