Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Python all() Function in Python
3.
Syntax
4.
Returns
5.
Python all() Function with Examples
5.1.
Example 1: Working of all() with Lists
5.2.
Python
5.3.
Example 2: Working of all() with Tuples
5.4.
Python
5.5.
Example 3: Working of all() with Sets
5.6.
Python
5.7.
Example 4: Working of all() with Dictionaries
5.8.
Python
5.9.
Example 5: Working of all() with Strings
5.10.
Python
6.
Frequently Asked Questions
6.1.
What happens if the iterable is empty?
6.2.
Can the all() function be used with user-defined objects?
6.3.
What is the difference between the all() and any() functions?
7.
Conclusion
Last Updated: Jul 31, 2024
Easy

Python all() Function

Author Pallavi singh
0 upvote

Introduction

Python is a versatile programming language that offers many built-in functions to make coding easier. One such function is the all() function. It is used to check if all the elements in an iterable are true or not. 

Python all() Function

In this article, we will learn about the all() function in Python, its syntax, return values, and see some examples of how to use it with different data types like lists, tuples, sets, dictionaries, and strings.

Python all() Function in Python

The all() function in Python takes an iterable (like a list, tuple, set, etc.) as an argument and returns True if all the elements in the iterable are true. If any of the elements are false or the iterable is empty, it returns False.

Here are some important points to remember about the all() function:

1. It returns True if all elements in the iterable are true.
 

2. It returns False if any element in the iterable is false.
 

3. It returns True if the iterable is empty.


The all() function is useful when you want to check if all the elements in an iterable satisfy a certain condition. For example, you can use it to check if all the numbers in a list are positive or if all the strings in a tuple are non-empty.

Syntax

The syntax of the all() function is:

all(iterable)


Here, "iterable" is the input iterable (like list, tuple, set, etc.) that you want to check.

Returns

The all() function returns:

1. True: If all elements in the iterable are true or the iterable is empty.
 

2. False: If any element in the iterable is false.

Python all() Function with Examples

Now let's see some examples of how to use the all() function with different data types.

Example 1: Working of all() with Lists

  • Python

Python

list1 = [True, True, True]

print(all(list1)) 


list2 = [True, False, True]

print(all(list2)) 


list3 = []

print(all(list3)) 

 

Output

True
False
True


In the first example, list1 contains all true elements, so all(list1) returns True. In the second example, list2 contains one false element, so all(list2) returns False. In the third example, list3 is an empty list, so all(list3) returns True.

Example 2: Working of all() with Tuples

  • Python

Python

tuple1 = (1, 2, 3)

print(all(tuple1))

tuple2 = (0, 1, 2)

print(all(tuple2)) 

tuple3 = ()

print(all(tuple3))


Output

True
False
True


In the first example, tuple1 contains all non-zero elements, which are considered true, so all(tuple1) returns True. In the second example, tuple2 contains one zero element, which is considered false, so all(tuple2) returns False. In the third example, tuple3 is an empty tuple, so all(tuple3) returns True.

Example 3: Working of all() with Sets

  • Python

Python

set1 = {1, 2, 3}

print(all(set1)) 



set2 = {0, 1, 2}

print(all(set2)) 



set3 = set()

print(all(set3)) 


Output

True
False
True


Similar to the examples with lists and tuples, in the first example, set1 contains all non-zero elements, so all(set1) returns True. In the second example, set2 contains one zero element, so all(set2) returns False. In the third example, set3 is an empty set, so all(set3) returns True.

Example 4: Working of all() with Dictionaries

  • Python

Python

dict1 = {1: 'a', 2: 'b', 3: 'c'}

print(all(dict1))


dict2 = {0: 'a', 1: 'b', 2: 'c'}

print(all(dict2)) 


dict3 = {}

print(all(dict3)) 

 

Output

True
False
True


When using all() with dictionaries, it checks the keys of the dictionary. In the first example, dict1 has all non-zero keys, so all(dict1) returns True. In the second example, dict2 has one zero key, so all(dict2) returns False. In the third example, dict3 is an empty dictionary, so all(dict3) returns True.

Example 5: Working of all() with Strings

  • Python

Python

str1 = "Hello"

print(all(str1)) 


str2 = ""

print(all(str2))


str3 = "False"

print(all(str3)) 


str4 = "0"

print(all(str4)) 


Output

True
True
True
True


When using all() with strings, it checks if all the characters in the string are non-empty. In the first example, str1 has all non-empty characters, so all(str1) returns True. In the second example, str2 is an empty string, so all(str2) also returns True. In the third example, str3 contains the word "False", but since it is a non-empty string, all(str3) returns True. Similarly, in the fourth example, str4 contains the character "0", which is also considered non-empty, so all(str4) returns True.

Frequently Asked Questions

What happens if the iterable is empty?

If the iterable passed to the all() function is empty, it returns True.

Can the all() function be used with user-defined objects?

Yes, the all() function can be used with user-defined objects. The objects should have a truth value defined for them using the bool() or len() method.

What is the difference between the all() and any() functions?

The all() function returns True if all elements in the iterable are true, while the any() function returns True if at least one element in the iterable is true.

Conclusion

In this article, we learned about the all() function in Python. We discussed its syntax, return values, and saw examples of how to use it with different data types like lists, tuples, sets, dictionaries, and strings. The all() function is a useful tool when you need to check if all elements in an iterable satisfy a certain condition.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass