map() FunctionApplies a given function to each item of an iterable (or multiple iterables) and returns an iterator of the results.
map(function, iterable, ...)
function: The function to apply to each item.iterable, ...: One or more iterables whose elements will be passed to the function.map object (an iterator). You often convert it to a list or other sequence.Example:
numbers = [1, 2, 3, 4]
squared = list(map(lambda x: x**2, numbers))
# squared is [1, 4, 9, 16]
words = ["apple", "banana", "cherry"]
lengths = list(map(len, words))
# lengths is [5, 6, 6]
filter() FunctionConstructs an iterator from elements of an iterable for which a function returns true.
filter(function, iterable)
function: A function that tests each element of the iterable, returning True or False. If None, elements that are "truthy" are kept.iterable: The iterable to filter.filter object (an iterator). You often convert it to a list or other sequence.Example:
numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, numbers))
# evens is [2, 4, 6]
words = ["apple", "banana", "cherry", "date"]
short_words = list(filter(lambda w: len(w) < 6, words))
# short_words is ['apple', 'date']
sorted() Function