Methods and Functions

Python all()

As a Python coder, you’ll constantly encounter Booleans and conditional expressions, which can be quite complex. In such cases, you should rely on a function that simplifies the reasoning. Just like Python any(), the Python all() method also provides similar capabilities. It iterates through an iterable and returns a single result indicating if any element is true in a Boolean context. Python all() is a conventional function that helps to reduce code length by replacing loops.

Python all() function

Definition

  • Python all() method is a built-in function that returns TRUE if all of the items of a provided iterable (List, Dictionary, Tuple, Set, etc.) are True; otherwise, it returns FALSE.
  • The Python all() function returns value TRUE if all the items in an iterable are true; else it returns FALSE.

Python all() Syntax

The syntax for Python all() is as follows:

all() Parameters

Python all() function accepts a single parameter:

iterable = can be an iterable object such as a list, tuple, dictionary, set, etc.

Return value from all()

The all() function returns a Boolean value:

TRUE if all of the elements in the iterable are true & also if the iterable is empty

FALSE if any element in an iterable is false

Condition Return Value
All values are True TRUE
All values are False FALSE
At least one value is True (others are False) FALSE
At least one value is False (others are True) FALSE
Iterable is Empty TRUE

All False values in Python

All of the following values are considered as False in Python:

  • Constants that are defined as false: FALSE and NONE.
  • A zero of any form of numeric value: Decimal(0), Fraction(0, 1), 0, 0.0, 0j
  • “, (), [], {}, set(), range(0) are examples of empty sequences and collections

Example 1: How all() works for lists?

If all of the elements in the list passed as an argument are true, only then does the Python all() function returns a TRUE value. To demonstrate the result of the all() function in various situations, we shall consider numerous lists with different entries.

Note – The Python all() function works similarly for tuples and sets just like the lists.

Example

                    

# Python program to illustrate all()
# all values are True
list1 = [1, 2, 3, 4]
print(all(list1))

# one value is False
list2 = [0, 2, 4]
print(all(list2))

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

# empty list
list4 = []
print(all(list4))

Output

                    

True
False
False
True

Explanation

In the first list, all the values are True so the all() function returns TRUE as its output. In the second case, there exists 1 False value and 2 True values. According to all(), the returned value for such a situation is FALSE. In the third list, both 0 and False are considered as False in Python. Hence the returned value is FALSE. In the final case, the list is kept empty. So, Python all() returns TRUE.

Example 2: How all() works for strings?

Any character, number, or text inside the quotation mark is considered as a TRUE value. Even if the string is empty, the all() function returns a TRUE value.

Example

                    

# Python program to illustrate all()
# all values are True
txt1 = 'Python Programming'
print(all(txt1))

# '0' is considered as True string
txt2 = '00'
print(all(txt2))

# empty string iterable
txt3 = ''
print(all(txt3))

Output

                    

True
True
True

Example 3: How all() works with Python dictionaries?

When you use the all() method on a dictionary, it only checks the keys, not the values. Only if all of the keys in the dictionary are true, then the all() function returns TRUE; otherwise, it returns FALSE. The all() function also returns a TRUE value when the dictionary is empty.

Example

                    

# all keys are True
dict1 = {1: 'Bread', 2: 'Cheese', 3: 'Eggs'}
print(all(dict1))

# one key is False
dict2 = {0: False, 1: 'True', 2: 'None'}
print(all(dict2))

# empty dictionary is True
dict3 = {}
print(all(dict3))

# a key of type string is True
dict4 = {'0': 'False'}
print(all(dict4))

Output

                    

True
False
True
True

Frequently Asked Questions

Q1. What is all() in Python?

Python all() method is a built-in function that returns TRUE if all of the items of a passed iterable (List, Dictionary, Tuple, Set, etc.) are True; otherwise, it returns FALSE. It iterates through an iterable and returns a single result indicating if any element is true in a Boolean context. Python all() is a conventional function that helps to reduce code length by replacing loops.

Q2. How do I use all() in Python?

The syntax for Python all() is as follows:

Python all() function accepts a single parameter:

iterable = can be an iterable object such as a list, tuple, dictionary, set, etc.

The all() function returns a Boolean value:

TRUE if all of the elements in the iterable are true & also if the iterable is empty

FALSE if any element in an iterable is false

Q3. Is there any() function in Python?

Python any(), an inbuilt function is used to iterate through an iterable/object and return a single result indicating if any element is true in a Boolean context. Python any() is a built-in function that is used for sequential And/Or. The Python any() function returns TRUE if at least one of the elements in the iterable is True, else it returns FALSE.

Q4. What happens when you use all() on a list?

When you pass a list to the Python all() function, it checks for the following conditions and based on it returns the Boolean value:

Condition Return Value
All values are True TRUE
All values are False FALSE
At least one value is True (others are False) FALSE
At least one value is False (others are True) FALSE
Iterable is Empty TRUE

Example

                    

# all the values in the list are True
list1 = ['Hey', 'Hello', 'Python', 'Program']
print(all(list1))

# one value is False, others are True
list2 = [0, 1, 2, 3]
print(all(list2))

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

# empty list
list4 = []
print(all(list4))

Output

                    

True
False
False
True

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.