Python all() Function - PythonForBeginners.com (2024)

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: TrueThe list is: [1, True, False]All the elements of the list evaluate to True: FalseThe 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: TrueThe list is: [1, True, False]All the elements of the list evaluate to True: FalseThe 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: PythonForBeginnersThe output is: TrueThe 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: TrueThe Set is: {False, 1, 2}All the elements of the set evaluate to True: FalseThe 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: TrueThe Dictionary is: {False: 1, 1: False}All the keys of the dictionary evaluate to True: FalseThe 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

Python all() Function - PythonForBeginners.com (2024)

References

Top Articles
2018 Jeep Wrangler Unlimited for sale - Shelbyville, TN - craigslist
Harvey's Barbers
Stadium Seats Near Me
Bucks County Job Requisitions
Bluegabe Girlfriend
Roblox Character Added
Oriellys St James Mn
Qhc Learning
1Win - инновационное онлайн-казино и букмекерская контора
Nonuclub
Evil Dead Rise Showtimes Near Regal Columbiana Grande
Craigslist Mpls Cars And Trucks
Lesson 8 Skills Practice Solve Two-Step Inequalities Answer Key
Viprow Golf
Midlife Crisis F95Zone
Kp Nurse Scholars
Craigslist Pet Phoenix
Christina Steele And Nathaniel Hadley Novel
Georgia Cash 3 Midday-Lottery Results & Winning Numbers
Southwest Flight 238
Craigslist Hunting Land For Lease In Ga
Tomb Of The Mask Unblocked Games World
Superhot Free Online Game Unblocked
Valley Craigslist
Evil Dead Rise Showtimes Near Regal Sawgrass & Imax
Kempsville Recreation Center Pool Schedule
Emily Katherine Correro
Gina's Pizza Port Charlotte Fl
Roch Hodech Nissan 2023
Microsoftlicentiespecialist.nl - Microcenter - ICT voor het MKB
Glossytightsglamour
Powerspec G512
Lovein Funeral Obits
Ukraine-Krieg - Militärexperte: "Momentum bei den Russen"
California Craigslist Cars For Sale By Owner
Pathfinder Wrath Of The Righteous Tiefling Traitor
Juiced Banned Ad
Honkai Star Rail Aha Stuffed Toy
UWPD investigating sharing of 'sensitive' photos, video of Wisconsin volleyball team
From Grindr to Scruff: The best dating apps for gay, bi, and queer men in 2024
Walmart Front Door Wreaths
Turok: Dinosaur Hunter
Here’s What Goes on at a Gentlemen’s Club – Crafternoon Cabaret Club
St Als Elm Clinic
Research Tome Neltharus
Glowforge Forum
Tamilblasters.wu
Tamilyogi Cc
Bob Wright Yukon Accident
Jesus Calling Oct 6
Qvc Com Blogs
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 6173

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.