• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Python all() Function

Python all() Function

Author: Aditya Raj
Last Updated: May 18, 2022

In python, we normally use the comparison operators and the logical operators to check for conditions for a different number of elements. What if you have to check for a condition in a list of elements? In this article, we will discuss the all() function in python. We will also see how we can use the all() function with different iterable objects.

Table of Contents
  • What is all() Function in Python?
  • The all() function with iterable objects
  • The all() Function With Dictionaries in Python
  • Conclusion

What is all() Function in Python?

The all() function is used to check if all elements in an iterable object evaluate to True or not. The all() function takes an iterable object like a list, tuple, set, dictionary, or string as its input argument. After execution, it returns True if all the elements of the iterable evaluate to True. Otherwise, it returns False. You can observe this in the following example.

myList1 = [1, 2, 3, 4]
myList2 = [1, True, False]
myList3 = []
print("The list is:", myList1)
output = all(myList1)
print("All the elements of the list evaluate to True:", output)
print("The list is:", myList2)
output = all(myList2)
print("All the elements of the list evaluate to True:", output)
print("The list is:", myList3)
output = all(myList3)
print("All the elements of the list evaluate to True:", output)

Output:

The list is: [1, 2, 3, 4]
All the elements of the list evaluate to True: True
The list is: [1, True, False]
All the elements of the list evaluate to True: False
The list is: []
All the elements of the list evaluate to True: True

You can understand the working of the all() function as an application of the and operator. For an iterable object having elements element1, element2, element3,.... elementN, using the all() function is equivalent to executing the statement element1 AND element2 AND element3 AND ….., AND elementN.

The all() function with iterable objects

When we pass a list as an input argument to the all() function, it returns True if all the elements of the list evaluate to True.

myList1 = [1, 2, 3, 4]
myList2 = [1, True, False]
myList3 = []
print("The list is:", myList1)
output = all(myList1)
print("All the elements of the list evaluate to True:", output)
print("The list is:", myList2)
output = all(myList2)
print("All the elements of the list evaluate to True:", output)
print("The list is:", myList3)
output = all(myList3)
print("All the elements of the list evaluate to True:", output)

Output:

The list is: [1, 2, 3, 4]
All the elements of the list evaluate to True: True
The list is: [1, True, False]
All the elements of the list evaluate to True: False
The list is: []
All the elements of the list evaluate to True: True

When we pass an empty list to the all() function, it returns True. However, if there is an element in the list that evaluates to False, the all() function returns False.

When we pass any string as an input argument to the all() function, it returns True.

myStr1 = "PythonForBeginners"
myStr2 = ""
print("The string is:", myStr1)
output = all(myStr1)
print("The output is:", output)
print("The string is:", myStr2)
output = all(myStr2)
print("The output is:", output)

Output:

The string is: PythonForBeginners
The output is: True
The string is: 
The output is: True

For an empty string, the all() function returns True.

Similar to lists, when we pass a set as an input argument to the all() function, it returns True if all the elements of the set evaluate to True.

mySet1 = {1, 2, 3, 4}
mySet2 = {1, 2, True, False}
mySet3 = set()
print("The Set is:", mySet1)
output = all(mySet1)
print("All the elements of the set evaluate to True:", output)
print("The Set is:", mySet2)
output = all(mySet2)
print("All the elements of the set evaluate to True:", output)
print("The Set is:", mySet3)
output = all(mySet3)
print("All the elements of the set evaluate to True:", output)

Output:

The Set is: {1, 2, 3, 4}
All the elements of the set evaluate to True: True
The Set is: {False, 1, 2}
All the elements of the set evaluate to True: False
The Set is: set()
All the elements of the set evaluate to True: True

When we pass an empty set to the all() function, it returns True.

The all() Function With Dictionaries in Python

When we pass a dictionary to the all() function as the input argument, it returns True if all the keys of the python dictionary evaluate to True. Otherwise, it returns False.

myDict1 = {1: 1, 2: 2, 3: 3, True: 4}
myDict2 = {False: 1, 1: 2, True: False}
myDict3 = {}
print("The Dictionary is:", myDict1)
output = all(myDict1)
print("All the keys of the dictionary evaluate to True:", output)
print("The Dictionary is:", myDict2)
output = all(myDict2)
print("All the keys of the dictionary evaluate to True:", output)
print("The Dictionary is:", myDict3)
output = all(myDict3)
print("All the keys of the dictionary evaluate to True:", output)

Output:

The Dictionary is: {1: 4, 2: 2, 3: 3}
All the keys of the dictionary evaluate to True: True
The Dictionary is: {False: 1, 1: False}
All the keys of the dictionary evaluate to True: False
The Dictionary is: {}
All the keys of the dictionary evaluate to True: True

When we pass an empty dictionary to the all() function, it returns True.

Conclusion

In this article, we have discussed the all() function in python. We also used the all() function with different iterable objects and observed the outputs of the function. To learn more about python programming, you can read this article on list comprehension.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us