English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python all() Usage and Examples

Python built-in functions

The all() method will return True when all elements in the given iterable are true. If not, it will return False.

The syntax of the all() method is:

all(iterable)

all() parameter

The all() method takes one parameter:

all() return value

The all() method returns:

  • True-If all elements of iterable are true

  • False-If any element of iterable is false

all() return value
Condition
Return value
All values are trueTrue
All values are falseFalse

A value is true (others are false)

False

A value is false (others are true)

False
Empty iterableTrue

Example1How to use all() with lists?

# All values are true
l = [1, 3, 4, 5]
print(all(l))
# All values are false
l = [0, False]
print(all(l))
# A false value
l = [1, 3, 4, 0]
print(all(l))
# A value is true
l = [0, False, 5]
print(all(l))
# Empty iterable
l = []
print(all(l))

The output when running the program is:

True
False
False
False
True

any() method is used in a similar way for tuples and similar listsSet.

Example2How to use all() with strings?

s = 'This is good'
print(all(s))
# 0 is False
# '0' is True
s = '000'
print(all(s))
s = ''
print(all(s))

The output when running the program is:

True
True
True

Example3How to use all() with Python dictionaries?

For dictionaries, if all keys (non-values) are true or the dictionary is empty, all() returns True. Otherwise, it returns false for all other cases.

s = {0: 'False', 1: 'False'}
print(all(s))
s = {1: 'True', 2: 'True'}
print(all(s))
s = {1: 'True', False: 0}
print(all(s))
s = {}
print(all(s))
# 0 is False
# '0' is True
s = {'0': 'True'}
print(all(s))

The output when running the program is:

False
True
False
True
True

Python built-in functions