all() Function Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Python provides various built-in functions that are very useful in performing complex tasks. So when you have to check if all items in an iterable evaluate to True, don't forget to use the all()all() function Python. So basically, all()all() function Python accepts an iterable object and returns True if all the items in the passed iterable are True, else it returns False. The iterable objects could be lists, tuples, dictionaries, sets, strings, etc.

Syntax of all() Function in Python

The syntax of all()all() function Python is:

Parameters of all() Function in Python

The all()all() function Python takes only single parameter:

Iterable: Any iterable object such as lists, tuples, dictionaries, etc that contains elements.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Return value of Python all() Function

The all()all() function Python returns:

  • True: If all the values in an iterable are True.
  • False: If any value in an iterable is False.

If we have an empty iterable, this function returns true.

How does all() Function in Python Work?

The all()all() function searches for false elements. It iterates every element in an iterable object, and when it reaches the end, that means everything is fine so it will return true; else if it encounters any false value, it will return false and stop iterating. Let’s have a look at one basic example that will show you the usage of the all()all() function.

As you can see in the above examples: In example 1, the all()all() function Python iterates till the end and returns True because there is no False value.

In example 2, the all()all() function Python will stop at the 0th0th index because 0 is False in a boolean context.

In example 3, the all()all() function Python will iterate till the end and will return True. Since the list is empty, it will print True.

Examples of all() Function Python

Example 1: How does all() Function Work for Lists?

Code:

Output:

Explanation: In the above code, as you can see,

We have created a list named l1, which has some numerical values. So when we use the all()all() function here, it will return True.

Then we created another list named l2. All the elements in the list l2 are False, so the all()all() function Python will generate a False boolean value. In l3, some elements are True while others are False. Because the list contains at least one false value, Python's all()all() function will return False. The last list(l4) is empty, so it will return True because there are no elements that are False in the list.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example 2: How does all() Function Work for Tuples?

Code:

Output:

Explanation: Here, we have defined four tuples. Some tuples have all True values and some all False, and some contain at least one False value. And the last tuple consists of an empty value. The all()all() function will take all the specified values as an argument and will return the result corresponding to each tuple.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Example 3: How does all() Function Work for a Set?

Code:

Output:

Explanation: The all()all() function works the same for sets, as it works for lists and tuples.

Example 4: How does all() Function Work for a Dictionary?

Code:

Output:

Explanation: As we can see in the above code, we have a pair of keys and values in the dictionary. So, when we apply the all()all() function to the dictionary, the output is solely dependent on the keys. In the dictionary d2, if all the keys of the dictionary or the dictionary are empty, the all()all() function in Python will return True, and if any key is False in the dictionary, then it will return False, as you can see in the dictionary d2.

Example 5: How does all() Function Work for a String?

Code:

Output:

Explanation: The string is also an iterable object. When we pass a string to the all()all() function Python, then it behaves like a list. So, the string always returns True, and if the string is empty, then it will also return True.

Consider str2. You might think str2 has 0 and should return false. The answer is NO because string always keeps any number or string in ""(inverted comma), so the output will always be true.

Conclusion

Let's conclude our topic all() function Python by mentioning some of the important points:

  • The all()all() function in Python returns True if all of the values in an iterable are True, and False if any of the values in an iterable are false.
  • The all()all() function takes a single iterable object and performs the operations. The iterable objects could be lists, tuples, dictionaries, sets, strings, etc.
  • The syntax of all()all() function Python is: all(iterable).
  • The all()all() function in Python either returns True or False, based on conditions.

See Also:

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more