Python all() function returns true if all values of the passed iterable object are true else it returns false.

Basically, all() Function in python checks whether all elements in the list, tuple, dictionary, etc is true or not.

Syntax:

all(iterable_object)

Python all() Parameters

iterable_object – Any iterable which can contain more than one elements e.g list, dictionary, set, tuple, etc

all() in Python returns only true or false depending on the elements.

Python all() Examples

Example 1: List

myList = [-1, 8, 4, 2]
print(all(myList))
True
myList = [-1, 8, 4, 2, 0]
print(all(myList))
False

Any integer except 0 is considered as true in Python.

Example 2: Dictionary

mydict = {4: "A", 2: "B", 3: "C"}
print(all(mydict))
True
mydict = {4: "A", 2: "B", 3: "C", 0: "D"}
print(all(mydict))
False

all() check keys instead of values for the dictionary.

Example 3: String

myStr = "Pencil Programmer"
print(all(myStr))

myStr = "0 to 9"
print(all(myStr))

myStr = "0"
print(all(myStr))
True
True
True

Though 0 in Python means false but '0' in Python is a character which has non-zero ASCII code therefore result into True.

Comment below your suggestion or doubts.

Leave a Reply

20 + fifteen =