PHP array_filter() Equivalent in Python

Python's filter() mirrors PHP's array_filter() in intent, but returns a lazy iterator, and idiomatic Python typically prefers a list comprehension with an `if` clause.

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

PHP source
array_filter()
Closest Python API
filter()
Idiomatic Python
list comprehension

Main limitation: PHP `array_filter()` with no callback removes "falsy" values (`false`, `0`, `""`, `null`, `[]`); Python's `filter(None, iterable)` does the equivalent, removing anything Python considers falsy — the exact falsy sets mostly overlap but PHP's `"0"` string is also falsy while Python's `"0"` string is truthy.

How to use each function

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

PHP: array_filter()

array_filter(array $array, ?callable $callback = null, int $mode = 0): array

Iterates over an array and keeps only the elements for which the callback returns a truthy value. With no callback, it removes all PHP-'falsy' elements (false, 0, '', null, []). The optional $mode flag lets the callback receive the key instead of, or in addition to, the value.

Returns: A new array containing only the elements that passed the test, preserving original keys.

PHP
$scores = [0, 85, false, 92, null, 78];
$valid = array_filter($scores);
// [1 => 85, 3 => 92, 5 => 78]

Python: filter()

filter(function, iterable)

Builds a lazy iterator over the items of iterable for which function returns a truthy value. Passing None as function drops every falsy item, similar to PHP's callback-less array_filter(). Like map(), the result is single-use — wrap it in list() to materialize it.

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

Python
evens = filter(lambda n: n % 2 == 0, [1, 2, 3, 4])
list(evens)  # [2, 4]

Quick mapping

PHP array_filter()
Closest Python API filter()
Idiomatic Python list comprehension

How it differs

  • PHP array_filter() with no callback removes "falsy" values (false, 0, "", null, []); Python's filter(None, iterable) does the equivalent, removing anything Python considers falsy — the exact falsy sets mostly overlap but PHP's "0" string is also falsy while Python's "0" string is truthy.
  • PHP array_filter() preserves the original array keys; Python's filter() (and comprehensions) only preserve order, not a key mapping — use {k: v for k, v in d.items() if ...} on a dict if you need to keep keys.
  • Python filter() returns a lazy iterator like map() — wrap it in list() to get a concrete list, and remember it can only be iterated once.

Examples

Keeping only even numbers

PHP
$evens = array_filter([1, 2, 3, 4], fn($n) => $n % 2 === 0);
// [1 => 2, 3 => 4]
Python
evens = list(filter(lambda n: n % 2 == 0, [1, 2, 3, 4]))
# [2, 4]
PHP keeps the original indices (1 and 3) in the result; Python's filter() produces a plain re-indexed list with no memory of original positions.

Idiomatic Python: list comprehension

PHP
$evens = array_filter([1, 2, 3, 4], fn($n) => $n % 2 === 0);
Python
evens = [n for n in [1, 2, 3, 4] if n % 2 == 0]
A comprehension with an `if` clause is the idiomatic Python equivalent and avoids the lazy-iterator gotcha of `filter()`.

Edge cases

Edge case

PHP's ARRAY_FILTER_USE_KEY and ARRAY_FILTER_USE_BOTH flags let the callback inspect keys, which Python's filter() cannot do directly — use a comprehension over .items() instead.

References

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

Last verified 2026-07-29.