all() in Python

Learn via video courses
Topics Covered

The all() function in python is an inbuilt function that evaluates whether all elements within an iterable object are truthy. It returns True if all elements are true, otherwise False. Iterable objects such as lists, tuples, sets, and dictionaries can be passed as arguments to all(). Even when the iterable is empty, all() yields True. Conversely, if any element in the iterable is falsy, all() returns False.

Syntax of all() in Python

The syntax of the all() in python is as follows:

Parameters of all() in Python

The all() function in python accepts a single parameter:

iterable: The iterable is considered an object such as a list, set, dictionary, tuple, etc.

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

Return Values of all() in Python

The return values of all() in python are: TRUE or FALSE.

True - The output is returned TRUE if all the elements in an iterable are TRUE. Even if the iterable object is empty while executing the all() function we get the output as TRUE.

False - The output is returned FALSE if any elements in an iterable are false.

For a better glance at all the possible scenarios, let us dive deeper below:

Possible ScenariosOutput Values
All iterable are trueTRUE
All iterable are falseFALSE
One iterable is true (others are false)FALSE
One iterable is false (others are true)FALSE
Empty IterableTRUE

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

Example

Output:

More Examples

Example 1: all() in Python with Lists

Output:

Example 2: all() in Python with Tuples

Output:

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 3: all() in Python with Sets

Output:

Example 4: all() in Python with Dictionaries

PRO TIP: Whenever we are dealing with all() function with Dictionaries, we need to remember that the keys are evaluated for the scenarios mentioned above. It means that if all the keys of the dictionary are true, then the output is true , or else it returns false.

Output:

Example 5: all() in Python with Strings

Whenever we are dealing with all() function with the iterable object as strings, we can have the following two scenarios majorly:

  1. NON- EMPTY Strings which give output as TRUE.
  2. EMPTY Strings which give output as TRUE.

PRO TIP: Whenever we deal with integers like 0, 1, 2..in strings as we keep them in "" (inverted commas), the output is always TRUE.

Let us dive into the example below to understand the concept explained above.

Output:

Example 6: all() function with Condition

Output:

Exceptions of all() in Python

As far as exceptions for all() in python are considered, we do need to keep in mind the following points:

  1. Whenever we are dealing with all() function with Dictionaries, we need to remember that the keys are evaluated for the scenarios mentioned above. It means that if all the keys in the dictionary are true, then the output is true. Otherwise, it returns false.
  2. Whenever we deal with integers like 0, 1, 2... in strings as we keep them in "" (inverted commas), the output is always TRUE.

Conclusion

  1. The all() in Python returns the output TRUE if all elements in the given iterable object are true.
  2. It works with various iterable objects, including lists, tuples, sets, and dictionaries. The function considers dictionary keys for evaluation, not values.
  3. The function evaluates elements in a boolean context. Non-zero, non-empty, or non-False values are considered True.
  4. For strings and numbers in strings, all() considers empty strings and strings with non-zero numbers as True.

See Also

  1. filter() in python
  2. any() in python
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more