PHP array_map() Equivalent in Python

Python's map() is the closest built-in match for PHP's array_map(), but it returns a lazy iterator, not a list — and idiomatic Python usually reaches for a list comprehension instead.

Close equivalent Covers the common case, but has documented behavioral differences.

PHP source
array_map()
Closest Python API
map()
Idiomatic Python
list comprehension

Main limitation: PHP `array_map()` returns a new array immediately; Python `map()` returns a lazy iterator that only produces values as you consume it (e.g. in a `for` loop or when passed to `list()`).

How to use each function

Before comparing the two, here's how array_map() and map() work on their own.

PHP: array_map()

array_map(callable $callback, array $array, array ...$arrays): array

Applies a callback to every element of one or more arrays and returns a new array of the results. When multiple arrays are passed, the callback receives one element from each array per call, and PHP pads any shorter array with null.

Returns: A new array with the same length as the longest input array.

PHP
$prices = [10, 20, 30];
$withTax = array_map(fn($p) => $p * 1.2, $prices);
// [12, 24, 36]

Python: map()

map(function, iterable, *iterables)

Applies function to every item of one or more iterables and returns a lazy map object — values are computed one at a time as they're consumed, not up front. Wrap the result in list() to materialize it immediately.

Returns: A map iterator; wrap in list(...) to get a concrete list.

Python
doubled = map(lambda n: n * 2, [1, 2, 3])
list(doubled)  # [2, 4, 6]

Quick mapping

PHP array_map()
Closest Python API map()
Idiomatic Python list comprehension

How it differs

  • PHP array_map() returns a new array immediately; Python map() returns a lazy iterator that only produces values as you consume it (e.g. in a for loop or when passed to list()).
  • PHP array_map() preserves the original string keys when called with a single array; Python has no direct equivalent for this since map() operates on plain iterables, not key-value structures — use a dict comprehension if key preservation matters.
  • PHP array_map() with a null callback zips multiple arrays together element-wise; Python's built-in zip() is the direct equivalent for that specific case, not map().

Examples

Doubling every element

PHP
$doubled = array_map(fn($n) => $n * 2, [1, 2, 3]);
// [2, 4, 6]
Python
doubled = list(map(lambda n: n * 2, [1, 2, 3]))
# [2, 4, 6]
Both produce the same result, but Python requires an explicit `list()` to force evaluation immediately, since `map()` alone only returns an iterator.

Idiomatic Python: list comprehension

PHP
$doubled = array_map(fn($n) => $n * 2, [1, 2, 3]);
Python
doubled = [n * 2 for n in [1, 2, 3]]
Most Python style guides (including PEP 8-adjacent community convention) prefer a list comprehension over `map()` with a lambda for simple transformations — it reads left-to-right and avoids the lazy-iterator gotcha entirely.

Edge cases

Edge case

array_map() on an empty array returns an empty array; Python's map() produces an empty iterator, and list(map(...)) on it correctly returns [] — no special-casing needed.

Edge case

If you need the mapped result to be reused multiple times, wrap the Python map() call in list() — iterating a map object a second time yields nothing, since iterators are single-use.

References

  • PHP Manual — array_map — checked 2026-07-29
  • Python docs — Built-in Functions — map() (v3.x) — checked 2026-07-29

Last verified 2026-07-29.