0
0
Pythonprogramming~5 mins

any() and all() functions in Python

Choose your learning style9 modes available
Introduction

The any() and all() functions help check if some or all items in a group meet a condition. They make it easy to test many things quickly.

Check if at least one light bulb in a string is working.
Verify if all students passed an exam.
Find out if any item in a shopping list is out of stock.
Confirm that all tasks in a to-do list are done.
Syntax
Python
any(iterable)
all(iterable)

iterable means a list, tuple, or any group of items you can check one by one.

any() returns True if at least one item is true. all() returns True only if every item is true.

Examples
any() is true because one item is true. all() is true because all items are true.
Python
any([False, True, False])  # Returns True
all([True, True, True])    # Returns True
any() is false because no item is true. all() is false because not all items are true.
Python
any([False, False, False])  # Returns False
all([True, False, True])    # Returns False
Empty list: any() returns false (nothing true), all() returns true (no false items).
Python
any([])  # Returns False
all([])  # Returns True
Sample Program

This program checks if any light is on, if all tasks are done, and shows results for empty lists.

Python
lights = [False, False, True, False]
print(any(lights))  # Is any light on?

tasks_done = [True, True, True]
print(all(tasks_done))  # Are all tasks done?

empty_list = []
print(any(empty_list))  # Check any in empty list
print(all(empty_list))  # Check all in empty list
OutputSuccess
Important Notes

Both functions work with any iterable, like lists, tuples, or sets.

They stop checking as soon as the result is known, so they are fast.

Remember: any([]) is False, all([]) is True.

Summary

any() checks if at least one item is true.

all() checks if every item is true.

Use them to quickly test groups of conditions.