BlogsDope image BlogsDope

Python all() function

Jan. 23, 2021 FUNCTION PYTHON 127

The Python library provides many useful in-built functions and all() function is one of them. This utility method an iterable  (list, tuple, dictionary etc.) as an argument and then returns True if all the elements in iterable are true, otherwise it returns False. If iterable passed is empty, then all() method returns True.

Python all() Syntax

The following is the syntax of the any() function: 
all(iterable)

Parameters:

The all method accepts Python iterables as its parameter values. Some of the iterables that are accepted by all() method include:
  • Lists, e.g. [45,50,True] 
  • Tuples, e.g. (52,64,17) 
  • Dictionaries, e.g. {70:"Dog", 71:"Cat"} 
  • Strings, e.g. "Ben", "Jacob".

Return value and equivalent code:

The all() function is an equivalent to a series of successive logical and operations on the passed iterable. Similiar to the logical and operation, the all() function returns True if all the values in the given iterable are True or if an empty iterable is passed, otherwise it returns False. One of the best features of the all() function is that it stops the execution as soon as the result is known.
Iterable values
all() method return value
All Truth values    ​
True
All False values
False
One Truth value and all others False
False
One False value and all others True
False
Empty iterable
True

The all() method is equivalent to the following code:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

Examples:

all() function with Lists:

# all values are true
# non-zero values are considered true
l1 = [5,65,8,12]
print(all(l1))

# all values are false
l2 = [0, False, 0]
print(all(l2))

# one value is false, others are true
l3 = [5,21,3,0,54]
print(all(l3))

# one value is true, others are false
l4 = [True,0,0,False]
print(all(l4))

# empty iterable 
l5 = []
print(all(l5))

Output:

True

False

False

False

True

all() function with Strings:

The all() function always returns a True value for purely string parameters.

# non-empty string
s1 = 'CodesDope'
print(all(s1))

# non-empty string
s2 = '0'
print(all(s2))

# empty-string
s3 = ''
print(all(s3))

Output:

True
True
True

all() function with Dictionaries:

In case of dictionaries, if all keys (not values) are true or the dictionary is empty, the all() function returns True, otherwise, it returns False.

# all keys are true
# 0 is False
# 1 is True
d1 = {5:'Fri', 6:'Sat', 7:'Sun'}
print(all(d1))

# all keys are false
d2 = {0:'False', False: 0}
print(all(d2))

# one value is false, others are true
d3 = {0:'Red', 1:'Blue',3:'Black'}
print(all(d3))

# one value is true, others are false
d4 = {1:'True',0:'False', False: 0}
print(all(d4))

# empty iterable - no elements
d5 = {}
print(all(d5))

Output:

True
False
False

False
True

all() function with Tuples:

# all values are true
t1 = (8,25,6,4,7)
print(all(t1))

# all values are false
t2 = (0, False, 0)
print(all(t2))

# one value is false, others are true
t3 = (0,2,3,4)
print(all(t3))

# one value is true, others are false
t4 = (0, 0, 1)
print(all(t4))

# empty iterable - no elements
t5 = ()
print(all(t5))

Output:

True
False
False
False
True

That sums up the features of the Python all() method.


Liked the post?
Zealous Learner.
Editor's Picks
0 COMMENT

Please login to view or add comment(s).