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

Tutoriel de base Python

Contrôle de flux Python

Fonctions en Python

Types de données en Python

Opérations sur les fichiers Python

Objets et classes Python

Dates et heures Python

Connaissances avancées Python

Manuel de référence Python

Méthode d'utilisation de Python all() et exemples

Python built-in functions

The all() method will return True when all elements of 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:

Return value of all()

The all() method returns:

  • True-If all elements of iterable are true

  • False-If any element of iterable is false

Return value of all()
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

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

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, for all other cases, it returns false.

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