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

Tutorial básico de Python

Control de flujo de Python

Funciones en Python

Tipos de datos en Python

Operaciones de archivos de Python

Objetos y clases de Python

Fecha y hora de Python

Conocimientos avanzados de Python

Manual de referencia de Python

Uso y ejemplo de Python all()

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))

When running the program, the output 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))

When running the program, the output 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))

When running the program, the output is:

False
True
False
True
True

Python built-in functions