Python all()

 The all() function in Python returns True if all the element of an iterable(Listsetdictionarytuple) is True. If not, it returns False. The all() method returns True if the iterable object is empty.

all() Syntax

The syntax of all() method is 

all(iterable)

all() Parameters

The all() function takes iterable as an argument the iterable can be of type listsettupledictionary, etc.

all() Return Value

The all() method returns a boolean value.

  • True if all of the elements in an iterable is true, In case of empty iterable object all() returns True.
  • False if any of elements in iterable are false
ConditionReturn Value
All elements are trueTrue
All elements are falseFalse
One element is true and others are false)False
One element is false and others are trueFalse
Empty IterableTrue

Difference between any() and all() functions in Python

You can roughly think of any() and all() as series of logical “OR” and “AND” operators in simple terms.

any

any() will return True when at least one of the elements is True. 

all

all() will return True only when all the elements are True.

Truth table

+-----------------------------------------+---------+---------+
|                                         |   any   |   all   |
+-----------------------------------------+---------+---------+
| All Truthy values                       |  True   |  True   |
+-----------------------------------------+---------+---------+
| All Falsy values                        |  False  |  False  |
+-----------------------------------------+---------+---------+
| One Truthy value (all others are Falsy) |  True   |  False  |
+-----------------------------------------+---------+---------+
| One Falsy value (all others are Truthy) |  True   |  False  |
+-----------------------------------------+---------+---------+
| Empty Iterable                          |  False  |  True   |
+-----------------------------------------+---------+---------+

The empty iterable case is explained in the official documentation as follows,

any – Return True if any element of the iterable is true. If the iterable is empty, return False

all – Return True if all elements of the iterable are true (or if the iterable is empty).

Example 1 – Using all() function on Python Lists

# All the elements in the list are true
list = [1,3,5,7]
print(all(list))

# All the elements in the list are false
list = [0,0,False]
print(all(list))

# Only one element is false
list = [1,5,7,False]
print(all(list))


# Only 1 element is true
list = [0, False, 5]
print(all(list))

# False since its Empty iterable 
list = []
print(all(list))

Output

True
False
False
False
True

Example 2 – Using all() function on Python Strings

# Non Empty string returns True
string = "Hello World"
print(all(string))

#  0 is False but the string character of 0 is True 
string = '000'
print(all(string))

# True since empty string and not iterable
string = ''
print(all(string))

Output

True
True
True

Example 3 – Using all() function on Python Dictionaries

In the case of a dictionary, if all the keys(not values) of the dictionary are true or if the dictionary is empty, then all() method returns True. Otherwise, it returns False.

# All elements in dictionary are true
dict = {1: 'Hello', 2: 'World'}
print(all(dict))

# All elements in dictionary are false
dict = {0: 'Hello', False: 'World'}
print(all(dict))


# Some of the elements in dictionary are true but one is false
dict = {1: 'Hello', 2: 'World', False: 'Welcome'}
print(all(dict))

# Empty Dictionary returns True
dict = {}
print(all(dict))

Output

True
False
False
True

Example 4 – Using all() function on Python Tuples

# All elements of tuple are true
t = (1, 2, 3, 4)
print(all(t))

# All elements of tuple are false
t = (0, False, False)
print(all(t))

# Some elements of tuple are true while others are false
t = (5, 0, 3, 1, False)
print(all(t))

# Empty tuple returns True
t = ()
print(all(t))

Output

True
False
False
True

Example 5 – Using all() function on Python Sets

# All elements of set are true
s = {1, 2, 3, 4}
print(all(s))

# All elements of set are false
s = {0, 0, False}
print(all(s))

# Some elements of set are true while others are false
s = {1, 2, 3, 0, False}
print(all(s))

# Empty set returns True
s = {}
print(all(s))

Output

True
False
False
True
0 Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
XOR in Python

XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are…