The all() function in Python is an inbuilt function which checks whether all elements in an iterable (such as string, list, etc.) are true or not.
This function returns True if all elements in the given iterable are non-zero (i.e. positive) or True. Otherwise, it returns False value.
Syntax of all() Function
The general syntax of the all() function is simple that is as:
all(iterable)
Parameters:
This function takes only one parameter “iterable” object that represents any sequence or collection of elements or items such as a string, list, tuple, set, or dictionary object.
Return type:
This method returns a boolean value:
- True: if all elements in the iterable are true.
- False: if at least one element in the iterable is false.
If the iterable is empty, all() returns True.
Examples based on all() Function in Python
Here are some important example programs that illustrate the usage of the all() method in different contexts.
Example 1: Using all() method with a list
# Creating an empty list. list = [] print(all(list)) # Returns True for empty list. # Create a list of positive and negative numbers. nums = [1, 2, 3, -4, -5, -6] print(all(nums)) # Returns True # Create a list of whole numbers. nums = [0, 1, 2, 3, 4, 5] print(all(nums)) # Returns False because of 0 # Create a list of mixed data type elements. data = [1, 'Python', 12.5, 5, False] print(all(data)) # Returns False because of False
Output: True True False False
Example 2:
Let’s take an example in which we will create a list of integers, and we will check if all of them are positive using all() method.
# Creating an empty list. nums = [10, 20, 30, 40, 50] all_positive = all(x > 0 for x in nums) print(all_positive)
Output: True
In this example program, we have created a list of numbers. Then, we have used a generator expression to create a sequence of boolean values that tell whether each element in the list nums is positive or not.
Then, we have passed this sequence to the all() method, which returns True because all elements in the list are positive. Finally, we have printed the result using the print() function on the console. If you run this code, it should output True, since all the numbers in the list are positive.
Example 3: Using all() method with strings
# Creating a string. str = "Scientech Easy" print(all(str)) # Creating an empty string. str2 = "" print(all(str2)) # Creating a string. str3 = "False" print(all(str3))
Output: True True True
Example 4: Using all() method with a tuple
Let’s take an example in which we will create a tuple of strings, and we will check if all elements in the tuple are non-zero.
# Creating a tuple. t = (10, 20, 30, "Scientech") print(all(t))
Output: True
In this example, we have created a tuple in which all elements are non-zero, the all() function has returned True value.
Example 5:
# Creating a tuple having one element is zero. t = (0, 5, 10, 15, 20) print(all(t)) # Create a tuple having one element is False. t2 = (20, 30, True, False) print(all(t2))
Output: False False
In the preceding program, one of the elements in the first tuple is zero. That’s why, the all() function returned False value. Similarly, in the second tuple, one of the elements is False. That’s why, it returned False value.
Example 6:
Let’s take an example in which we will create a tuple of strings, and check if all of them begin with the letter “S” using all() method.
# Creating a tuple of elements. fruits = ('Strawberry', 'Sour cherry', 'Sultana grape') all_start_with_S = all(x.startswith('S') for x in fruits) print(all_start_with_S)
Output: True
In this example, we first have defined a tuple of fruits with the names ‘Strawberry’, ‘Sour cherry’, and ‘Sultana grape’. We then use a generator expression with the all() function to check whether all the fruits in the tuple begin with the letter ‘S’.
The generator expression x.startswith(‘S’) for x in fruits returns a boolean value for each fruit, telling whether it starts with ‘S’ or not.
The all() function then returns True if all the boolean values are True, indicating that all fruits in the tuple start with ‘S’. Finally, we have printed the result using the print() function on the console.
If you run this code, it should output True, as all three fruits in the tuple begin with the letter ‘S’.
Example 7: Using all() function with a dictionary
# Creating two dictionaries. nums_dict = {1:'One', 2:'Two', 3:'Three', 4:'Four'} print(all(nums_dict)) # Returns True nums_dict = {0:'zero', 1:'One', 2:'Two', 3:'Three', 4:'Four'} print(all(nums_dict)) # Returns False for 0
Output: True False
In this tutorial, you have learned about how to use all() function in Python with various examples. Hope that you will have understood the basic concepts of all() function.
Thanks for reading!!!