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
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Lstrip()

Python String lstrip()

Table of Contents Hide lstrip() Syntaxlstrip() Parameterslstrip() Return ValueExample 1: Working of lstrip()Example 2 – How to use lstrip() method in real world? The Python String lstrip() method is a…
View Post
Python String Center()

Python String center()

Table of Contents Hide center() Syntaxcenter() Parameter center() Return ValueExample 1: center() Method With Default fillcharExample 2: center() Method With * fillchar Example 3: Returns original string if the width is…
View Post
Python String Find()

Python String find()

Table of Contents Hide find() Syntaxfind() Parametersfind() Return ValueDifference between find() method and index() methodExample 1: find() without any argumentsExample 2: find() with start and end Arguments The Python String…
View Post
Python String Capitalize()

Python String capitalize()

Table of Contents Hide capitalize() Syntaxcapitalize() ParameterReturn Value from capitalize()Example: Capitalize a string in Python Python string capitalize() method will convert the first letter in a string to uppercase and keep…
View Post
Python List Append()

Python List append()

Table of Contents Hide Syntax of List append() append() ParametersReturn Value from List append()Example 1: Adding Element to a ListExample 2: Adding List into the List The append() method in Python…
View Post