AppDividend
Latest Code Tutorials

Python all: How to Use all() Method in Python

Python all() is an inbuilt function Example that returns True when all items in the given iterable are true, otherwise, it returns False. If the iterable object is empty, the all() function also returns True.

Python all() Function

The all() method returns:

  1. True – If all elements in an iterable are true
  2. False – If any element in an iterable is false

See the following syntax.

all(iterable)

The all() method takes a single parameter. An iterable parameter is an object which can be list, tuple, or dictionary.

The all() method returns:

  1. True – If all the items in an iterable are True.
  2. False – If any item in an iterable is False.
WhenReturn Value
All values are trueTrue
All values are falseFalse
One value is true (others are false)False
One value is false (others are true)False
Empty IterableTrue

#The all() function for Python List

Let’s test the all() function for Python List.

# app.py

listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
print(all(listB))

See the following output.

➜  pyt python3 app.py
True
➜  pyt

Let’s check for the empty iterable.

# app.py

listB = []
print(all(listB))

See the output.

➜  pyt python3 app.py
True
➜  pyt

Let’s take the false values inside the list and check the output.

# app.py

listB = [0, False]
print(all(listB))

See the following output.

➜  pyt python3 app.py
False
➜  pyt

Let’s take one of the values as false.

# app.py

listB = [21, 19, 18, 0]
print(all(listB))

See the output.

➜  pyt python3 app.py
False
➜  pyt

#The all() function for Python Strings

Strings are also iterators in Python. So, let’s test with all() function.

# app.py

strB = 'MillieBobbyBrown'
print(all(strB))

See the output.

➜  pyt python3 app.py
True
➜  pyt

See other examples.

# app.py

strC = '02119'
print(all(strC))

strD = ''
print(all(strD))

See the output.

➜  pyt python3 app.py
True
True
➜  pyt

#The all() function for Python Dictionary

In the case of dictionaries, if all the keys (not values) are True or a dictionary is empty, all() returns True. Else, it returns the False value for all other cases.

See the following code example.

dictA = {0: 'False', 1: 'False'}
print(all(dictA))

dictB = {1: 'True', 21: 'True'}
print(all(dictB))

dictC = {1: 'True', False: 0}
print(all(dictC))

dictD = {}
print(all(dictD))

dictE = {'0': 'True'}
print(all(dictE))

Okay, so in the above code, if the keys are either 0 or False, then the output will be False. 

#Python’s any() and all() functions

The function any() and all(), both look for the condition that allows them to stop evaluating.

Here’s the Python implementation of any() and all() functions.

def any(iterable):
    for i in iterable:
        if i:
            return True
    return False # for an empty iterable, any returns False!

def all(iterable):
    for i in iterable:
        if not i:
            return False
    return True  # for an empty iterable, all returns True!

Finally, Python all() Function Example Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.