BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python all() Function with examples

Last Updated: March 16, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

Python all() function accepts an iterable object (such as list, dictionary etc.) as an argument. If all the elements in the passed iterable are true then all() function returns true else it returns false. If the iterable is empty then also this function returns true.

Python all() function works on?

The all() function can be used on any iterable such as lists, tuples, dictionaries etc.

Python all() function example with lists

In this example, we will see how the all() function works with the lists. We have taken several lists with different values to demonstrate the output of all() function in different scenarios. The 0 (zero) values in lists are considered as false and the non-zero values such as 1, 20 etc are considered true.

# all values of this list are true
# non-zero values are considered true
lis1 = [10, 5, 15, 77]
print(all(lis1))

# all values are false
# 0 is considered as false
lis2 = [0, False, 0]
print(all(lis2))

# one value is false, others are true
lis3 = [10, 0, 40]
print(all(lis3))

# one value is true, others are false
lis4 = [0, 0, True]
print(all(lis4))

# empty iterable - no elements
lis5 = []
print(all(lis5))

Output:
Python all() function with lists

Python all() function example with Dictionary

# Both the keys are true
dict1 = {1: 'True', 2: 'False'}
print(all(dict1))

# One of the key is false
dict2 = {0: 'True', 1: 'True'}
print(all(dict2))

# Both the keys are false
dict3 = {0: 'True', False: 0}
print(all(dict3))

# Empty dictionary
dict4 = {}
print(all(dict4))

# Note: Here the key is actually true because
#  '0' is a non-null string rather than a zero
dict5 = {'0': 'True'}
print(all(dict5))

Output:
Python all function dictionary example

Python all() function example with Strings

# non-null string
s1 = "Hello World"
print(all(s1))

# non-null String
s2 = '0'
print(all(s2))

# non-null String
s3 = ''
print(all(s3))

# non-null String
s4 = 'False'
print(all(s4))

Output:

True
True
True
True

Python all() example with tuples

# all true values
t1 = (1, 2, 3, 4, 5)
print(all(t1))

# one false value
t2 = (0, 1, "Hello")
print(all(t2))

# all false values
t3 = (0, False , 0)
print(all(t3))

# one true value, all false
t4 = (True, 0, False)
print(all(t4))

Output:
Python all function example with tuples

Top Related Articles:

  1. Python Variables with examples
  2. Python abs() Function with examples
  3. Python Keywords and Identifiers with examples
  4. Python Recursion
  5. Python Set intersection_update() method with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap