Python all() Function with Examples

The all() is one of the built-in python functions which uses iterable objects to return the value True or False. It returns True if all the elements..

Here's a tutorial in which we will discuss about the all() function in python programming language with the help of examples.

Python all() function

Python all() function

The is one of the built-in python functions which uses iterable objects to return the value or . It returns True if all the elements of the specified iterable are true and False if all the elements of the specified iterable are false. The iterable objects can be strings, list, tuple, dictionary, etc.

Syntax of Python all() function:

all(iterable)

Parameter of all() function in python

The python function takes only one argument i.e. iterables.

Iterables - Any type of iterable like tuple, list, dictionary, etc. which includes all the elements.

Return Value of Python all() function

The python function returns the following values;

True - It returns True if all the elements of the specified iterable are true.

False - It returns False if all the elements of the specified iterable are false.

Example 1: How Python all() works for Lists?

# Non-zero values are always considered true by the compiler

li1 = [14, 2, 12, 9]

print(all(li1))

# all values are false

# 0 is considered as false

li2 = [0, False, 0]

print(all(li2))

# one value is false, rest are true

li3 = [14, 0, 47]

print(all(li3))

# one value is true, rest are false

li4 = [True, 0, 0]

print(all(li4))

# empty iterable

li5 = []

print(all(li5))

Output

True

False

False

False

True

Example 2: How Python all() works for Tuples?

# all values are true

tup1 = (14, 92, 2, 10, 7)  

print(all(tup1))

# one value is false rest are true

tup2 = (0, 14, "Answersjet")  

print(all(tup2))

# all values are false

tup3 = (0, False, 0)  

print(all(tup3))

# one value is true, rest are false  

tup4 = (True, 0, False)

print(all(tup4))  

# empty tuple

tup5 = ()

print(all(tup5))

Output

True

False

False

False

True

Example 3: How Python all() works for Dictionary? 

# both the keys are true

dic1 = {7: 'True', 4: 'False'}

print(all(dic1))

# One of the key is false rest are true

dic2 = {0: 'True', 1: 'True', 6: 'Hello'}

print(all(dic2))

# Both the keys are false

dic3 = {0: 'Answersjet', False: 0}

print(all(dic3))

# Empty dictionary

dic4 = {}

print(all(dic4))

# Above key is true because '0' is a non-null string rather than a zero

dic5 = {'0': 'True'}

print(all(dic5))

Output

True

False

False

True

True

Example 4: How Python all() works for Strings?

# non-null Strings

str = "Answersjet"

print(all(str))

# 0 is False

# '0' is True

str = '000'

print(all(str))

# empty Strings

str = ''

print(all(str))

Output

True

True

True