Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

map

map() returns an iterator that applies a function to items from one or more iterables. With multiple inputs, it processes corresponding items in parallel and can require equal lengths with strict=True.

How it works

map() creates a lazy iterator that calls a function for each item in one iterable, or for corresponding items across several iterables. With multiple iterables, the callback receives one item from each; iteration normally ends when the shortest input is exhausted, while strict=True rejects unequal lengths.

Syntax

map(function, iterable, /, *iterables, strict=False)

Parameters

NameTypeDescription
functioncallableCallable applied to each item, receiving one argument per iterable.
iterableiterablePrimary iterable supplying items to transform.
iterablesiterable, ...Optional additional iterables consumed in parallel.
strictboolIf true, raise ValueError when parallel iterables have different lengths.

Returns

An iterator yielding the function results.

Key points

  • map() lazily transforms each item and yields the callback’s return value.
  • Adding input iterables increases the callback’s required arity by the same number.
  • strict=True provides an explicit length-consistency check for parallel iteration.

Common mistakes

  • Multiple inputs are silently truncated to the shortest one unless strict=True is used.
  • Use strict=True when unequal input lengths should be treated as an error.
  • Use itertools.starmap() when each input item is already a tuple of callback arguments.

Example: Map values from parallel iterables

numbers = [1, 2, 3]
factors = [10, 20, 30]
scaled = list(map(lambda number, factor: number * factor, numbers, factors))
print(scaled)  # [10, 40, 90]

A list materializes the iterator so its mapped results can be reused.

When to use it

Lazy transformation

Convert or normalize each value without constructing an intermediate list immediately.

Parallel processing

Combine aligned records, keys, or measurements by passing several iterables to one callback.

Strict length checking

Validate that parallel inputs contain the same number of items.

Interview questions

QWhat does map() return, and when does it apply the function?

map() returns an iterator rather than an eagerly built list. The callback is applied as results are requested, so wrapping it in list() consumes the iterator and materializes the values.

Follow-up: How can laziness affect repeated use? An iterator is consumed during iteration, so create a new map object or store the materialized results if multiple passes are required.

QHow does map() behave with multiple iterables?

With extra iterables, the callback must accept one argument for each iterable. Items are taken in parallel, and ordinary map() stops as soon as the shortest iterable is exhausted.

Follow-up: How can mismatched lengths be detected? Pass strict=True; Python raises ValueError if any iterable ends before the others.

QWhen should strict=True or itertools.starmap() be used?

The function argument is positional-only, and additional iterables are supplied after the primary iterable. The current signature also provides strict=False to control unequal-length handling.

Follow-up: When is itertools.starmap() preferable? Use it when each source item is already an argument tuple that should be unpacked into the function call.

  • itertools.starmap()
  • filter()
  • generator expressions

Source: Built-in functions. Checked 2026-10-15.