Fundamentals 7 min read

Common Python Built‑in Functions: map, filter, reduce, sorted and More

This article introduces essential Python built‑in functions such as map, filter, reduce, sorted, any, all, zip, enumerate, reversed, chr, max, min, sum, join, list, tuple, set, frozenset, type and dir, explaining their purpose and providing concise example code for each.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Common Python Built‑in Functions: map, filter, reduce, sorted and More

map(function, iterable, ...) applies a function to each item of an iterable and returns an iterator of results. def square(x): return x ** 2 numbers = [1, 2, 3, 4, 5] squared_numbers = map(square, numbers) print(list(squared_numbers)) # [1, 4, 9, 16, 25]

filter(function, iterable) returns an iterator containing only the elements for which the function returns True. def is_even(x): return x % 2 == 0 numbers = [1, 2, 3, 4, 5, 6] even_numbers = filter(is_even, numbers) print(list(even_numbers)) # [2, 4, 6]

reduce(function, iterable[, initializer]) cumulatively applies a binary function to the items of an iterable, optionally starting with an initializer. from functools import reduce numbers = [1, 2, 3, 4, 5] sum_result = reduce(lambda x, y: x + y, numbers) print(sum_result) # 15

sorted(iterable[, key, reverse]) returns a new list containing all items from the iterable in sorted order, with optional key and reverse arguments. words = ["apple", "orange", "banana", "cherry"] sorted_words = sorted(words, key=len) print(sorted_words) # ['apple', 'cherry', 'banana', 'orange']

any(iterable) returns True if at least one element of the iterable is true, otherwise False. numbers = [0, False, "", None] has_true_value = any(numbers) print(has_true_value) # False

all(iterable) returns True only if every element of the iterable is true, otherwise False. bool_values = [True, True, True] all_true = all(bool_values) print(all_true) # True

zip(*iterables) aggregates elements from each of the iterables into tuples. names = ["Alice", "Bob", "Charlie"] ages = [25, 30, 35] name_age_pairs = zip(names, ages) print(list(name_age_pairs)) # [('Alice', 25), ('Bob', 30), ('Charlie', 35)]

enumerate(iterable[, start]) yields pairs containing a count (from start) and the corresponding value from the iterable. fruits = ["apple", "banana", "cherry"] indexed_fruits = enumerate(fruits) print(list(indexed_fruits)) # [(0, 'apple'), (1, 'banana'), (2, 'cherry')]

reversed(seq) returns an iterator that accesses the given sequence in the reverse order. numbers = [1, 2, 3, 4, 5] reversed_numbers = reversed(numbers) print(list(reversed_numbers)) # [5, 4, 3, 2, 1]

chr(i) returns the string representing a character whose Unicode code point is the integer i. code_point = 97 character = chr(code_point) print(character) # 'a'

max(iterable[, default=obj, key=func]) returns the largest item in an iterable. numbers = [1, 2, 3, 4, 5] largest_number = max(numbers) print(largest_number) # 5

min(iterable[, default=obj, key=func]) returns the smallest item in an iterable. numbers = [1, 2, 3, 4, 5] smallest_number = min(numbers) print(smallest_number) # 1

sum(iterable[, start]) returns the total of all elements in the iterable, optionally adding a start value. numbers = [1, 2, 3, 4, 5] total_sum = sum(numbers) print(total_sum) # 15

str.join(iterable) concatenates the string representations of an iterable's items using the string as a separator. words = ["apple", "banana", "cherry"] joined_words = "-".join(words) print(joined_words) # apple-banana-cherry

list(iterable) converts an iterable into a list. range_iterator = range(5) numbers_list = list(range_iterator) print(numbers_list) # [0, 1, 2, 3, 4]

tuple(iterable) converts an iterable into a tuple. numbers_list = [1, 2, 3, 4, 5] numbers_tuple = tuple(numbers_list) print(numbers_tuple) # (1, 2, 3, 4, 5)

set(iterable) creates a set from an iterable, removing duplicate elements. numbers_list = [1, 2, 3, 2, 1, 4, 5] unique_numbers_set = set(numbers_list) print(unique_numbers_set) # {1, 2, 3, 4, 5}

frozenset(iterable) creates an immutable set from an iterable, also removing duplicates. numbers_list = [1, 2, 3, 2, 1, 4, 5] unique_numbers_frozenset = frozenset(numbers_list) print(unique_numbers_frozenset) # frozenset({1, 2, 3, 4, 5})

type(obj) returns the type of the given object. number = 123 number_type = type(number) print(number_type) #

dir(obj) returns a list of the attributes and methods of the object. import os os_dir = dir(os) print(os_dir) # ['__all__', '__builtins__', '__cached__', ...]

pythonData StructuresTutorialprogramming fundamentalsiterationbuilt-in-functions
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.