Any All With Generator in Python

Level 3126 keystrokes

This uses all() and any() with generator expressions for efficient checks.

Python code

# All five numbers are even
1nums = [2, 4, 6, 8, 10]
# all() is True only if every n % 2 == 0 holds; it stops at the first False
2all_even = all(n % 2 == 0 for n in nums)
# any() is True if at least one n > 5; it stops at the first True (6)
3any_gt_5 = any(n > 5 for n in nums)
# Prints True True
4print(all_even, any_gt_5)

Output

True True
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • _× 6
  • ,× 5
  • =× 5
  • (× 3
  • )× 3
  • [× 1
  • ]× 1
  • %× 1
  • >× 1

More Python examples at level 3

← Python typing practice