Table of Contents
In this tutorial, we will learn about the Python all() function with the help of examples.
The all()
function returns True
if all elements in the given iterable are true. If not, it returns False
.
Example
boolean_list = ['True', 'True', 'True'] # check if all elements are true result = all(boolean_list) print(result) # Output: True
1. all() Syntax
The syntax of the all()
function is:
all(iterable)
2. all() Parameters
The all()
function takes a single parameter:
- iterable – any iterable (list, tuple, dictionary, etc.) which contains the elements
3. all() Return Value
all()
function returns:
- True – If all elements in an iterable are true
- False – If any element in an iterable is false
When | Return Value |
---|---|
All values are true | True |
All values are false | False |
One value is true (others are false) | False |
One value is false (others are true) | False |
Empty Iterable | True |
4. Example 1: How all() works for lists?
# all values true l = [1, 3, 4, 5] print(all(l)) # all values false l = [0, False] print(all(l)) # one false value l = [1, 3, 4, 0] print(all(l)) # one true value l = [0, False, 5] print(all(l)) # empty iterable l = [] print(all(l))
Output
True False False False True
The all()
function works in a similar way for tuples and sets like lists.
5. Example 2: How all() works for strings?
s = "This is good" print(all(s)) # 0 is False # '0' is True s = '000' print(all(s)) s = '' print(all(s))
Output
True True True
6. Example 3: How all() works with Python dictionaries?
In the case of dictionaries, if all keys (not values) are true or the dictionary is empty, all() returns True. Else, it returns false for all other cases..
s = {0: 'False', 1: 'False'} print(all(s)) s = {1: 'True', 2: 'True'} print(all(s)) s = {1: 'True', False: 0} print(all(s)) s = {} print(all(s)) # 0 is False # '0' is True s = {'0': 'True'} print(all(s))
Output
False True False True True
Related posts:
Python Operators
Python Anonymous / Lambda Function
Python Program to Check if a Number is Odd or Even
Python Program to Remove Duplicate Element From a List
Python Data Structures and Algorithms - Benjamin Baka
Python String isspace()
Python File I/O Operation
Python Program to Get the Full Path of the Current Working Directory
Python Program to Create a Long Multiline String
Python Program to Convert Two Lists Into a Dictionary
Python Program Read a File Line by Line Into a List
Python Program to Make a Simple Calculator
APIs in Node.js vs Python - A Comparison
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Machine Learning - Sebastian Raschka
Python Directory and Files Management
Machine Learning with Python for everyone - Mark E.Fenner
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Program to Differentiate Between del, remove, and pop on a List
Python String title()
Python String format()
Python chr()
Python Program to Convert Celsius To Fahrenheit
Python Dictionary popitem()
Python String ljust()
Python datetime
Python Program to Iterate Through Two Lists in Parallel
Python Dictionary keys()
Python Program to Get File Creation and Modification Date
Deep Learning with Python - Francois Chollet
Python List pop()
Python Program to Remove Punctuations From a String