Skip to content

Python all() function

  • by

Python all() function is used to check if all elements in the given iterable are true. This function returns True if all items in an iterable are true, otherwise, it returns False.

all(iterable)

Python all() function takes a single parameter. That accepts an iterable object (such as a list, dictionary, etc.).

Python all function example

Simple example code check if all items in a list are True.

mylist = [True, True, True]
print(all(mylist))

mylist = [0, 1, 1]
print(all(mylist))

Output:

Python all() function

strings

s = "This is good"
print(all(s)) #true

# 0 is False
# '0' is True
s = '000'
print(all(s)) #true

s = ''
print(all(s)) #true

dictionaries

s = {0: 'False', 1: 'False'}
print(all(s)) #false

s = {1: 'True', 2: 'True'}
print(all(s)) #true

set

myset = {0, 1, 0}
print(all(myset)) #false

tuple

mytuple = (0, True, False)
print(all(mytuple)) #false

Do comment if you have any doubts or suggestions on this Python function code.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading