Python all() Function with Examples

In the previous article, we have discussed Program for Python String swapcase() Function
all() Function in Python:

The function all() returns If all of the items in an iterable are true, it returns True; otherwise, it returns False.

The all() function returns True if the iterable object is empty.

Syntax:

all(iterable)

Parameters

iterable: It may be any iterable object like list, tuple, dictionary, set, etc.

Note: When applied to a dictionary, the all() function determines whether all of the keys are true, rather than the values.

Return value:

The function all() returns:

  • True if all of the elements in an iterable are true.
  • If any element in an iterable is false, then it returns false.

Note:

when 0 is given in the form as string, then it returns true
0 = False
'0' = True
for example: number = '00000'
# output = True

Examples:

Example1:

Input:

Given list =  [0, 1, 2, 3]
Given tuple = (10, 30, -10)
Given dictionary = {0: 'good', 1: 'morning'}

Output:

The result after applying all() function on the given list =  False
The result after applying all() function on the given tuple =  True
The result after applying all() function on the given dictionary =  False

Example2:

Input:

Given list =  ['True', 'True', 'True']
Given tuple = ()
Given dictionary = {3: 'welcome', 2: 'to', 1: 'Python-programs'}

Output:

The result after applying all() function on the given list =  True
The result after applying all() function on the given tuple =  True
The result after applying all() function on the given dictionary =  True

Program for all() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Give the tuple as static input and store it in another variable.
  • Give the dictionary as static input and store it in another variable.
  • Apply all() function on the given list that returns true if all of the items in an iterable are true, otherwise, it returns False.
  • Store it in another variable.
  • Apply all() function on the given tuple and store it in another variable.
  • Apply the all() function on the given dictionary and store it in another variable.
  • Print the result after applying all() the function on the given list.
  • Print the result after applying all() the function on the given tuple.
  • Print the result after applying all() the function on the given dictionary.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [0, 1, 2, 3]
# Give the tuple as static input and store it in another variable.
gvn_tupl = (10, 30, -10)
# Give the dictionary as static input and store it in another variable.
gvn_dictnry = {0: 'good', 1: 'morning'}
# Apply all() function on the given list that returns true if all of the
# items in an iterable are true, otherwise, it returns False.
# Store it in another variable.
rslt_lst = all(gvn_lst)
# Apply all() function on the given tuple and store it in another variable.
rslt_tupl = all(gvn_tupl)
# Apply all() function on the given dictionary and store it in another variable.
rslt_dictnry = all(gvn_dictnry)
# Print the result after applying all() function on the given list.
print("The result after applying all() function on the given list = ", rslt_lst)
# Print the result after applying all() function on the given tuple.
print("The result after applying all() function on the given tuple = ", rslt_tupl)
# Print the result after applying all() function on the given dictionary.
print("The result after applying all() function on the given dictionary = ", rslt_dictnry)

Output:

The result after applying all() function on the given list =  False
The result after applying all() function on the given tuple =  True
The result after applying all() function on the given dictionary =  False

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions and store it in a variable.
  • Give the tuple user input using list(),map(),input(),and split() functions and store it in another variable.
  • Take a dictionary and initialize it with an empty dictionary using dict() or {}.
  • Give the number of keys as user input using int(input()) and store it in a variable.
  • Loop till the given number of keys using for loop.
  • Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
  • Initialize the key with the value of the dictionary.
  • Apply all() function on the given list that returns true if all of the items in an iterable are true, otherwise, it returns False.
  • Store it in another variable.
  • Apply all() function on the given tuple and store it in another variable.
  • Apply the all() function on the given dictionary and store it in another variable.
  • Print the result after applying all() the function on the given list.
  • Print the result after applying all() the function on the given tuple.
  • Print the result after applying all() the function on the given dictionary.
  • The Exit of the Program.

Below is the implementation:

# Give the list as User input using list(),map(),input(),and split() functions
# and store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the tuple as User input using list(),map(),input(),and split() functions
# and store it in another variable.
gvn_tupl = tuple(map(int, input(
    'Enter some random Tuple Elements separated by spaces = ').split()))
# Take a dictionary and initialize it with an empty dictionary using dict() or {}.
gvn_dict = {}
# Give the number of keys as user input using int(input()) and store it in a variable.
numb_of_kys = int(
    input('Enter some random number of keys of the dictionary = '))
# Loop till the given number of keys using for loop.
for p in range(numb_of_kys):
        # Inside the for loop scan the key and value as
    # user input using input(),split() functions
    # and store them in two separate variables.
    keyy, valuee = input(
        'Enter key and value separated by spaces = ').split()
    # Initialize the key with the value of the dictionary.
    gvn_dict[int(keyy)] = valuee

# Apply all() function on the given list that returns true if all of the
# items in an iterable are true, otherwise, it returns False.
# Store it in another variable.
rslt_lst = all(gvn_lst)
# Apply all() function on the given tuple and store it in another variable.
rslt_tupl = all(gvn_tupl)
# Apply all() function on the given dictionary and store it in another variable.
rslt_dictnry = all(gvn_dict)
# Print the result after applying all() function on the given list.
print("The result after applying all() function on the given list = ", rslt_lst)
# Print the result after applying all() function on the given tuple.
print("The result after applying all() function on the given tuple = ", rslt_tupl)
# Print the result after applying all() function on the given dictionary.
print("The result after applying all() function on the given dictionary = ", rslt_dictnry)

Output:

Enter some random List Elements separated by spaces = 10 25 35 0
Enter some random Tuple Elements separated by spaces = 1 1 1 1
Enter some random number of keys of the dictionary = 3
Enter key and value separated by spaces = 3 welcome
Enter key and value separated by spaces = 2 to
Enter key and value separated by spaces = 1 Python-programs
The result after applying all() function on the given list = False
The result after applying all() function on the given tuple = True
The result after applying all() function on the given dictionary = True

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.