Python all()

Python all() takes a sequence of items, such as a list, tuple, or string, and checks if every single item in that sequence evaluates to True. If all items are True, the function returns True. However, if it finds even one False item or the sequence is empty, it immediately returns False.
Table of Contents

Understanding Python all() Function

Python all() is a built-in function that helps you check if all elements in a collection are considered True. It checks each element in an iterable (like a list, tuple, or string) and returns True only if all the elements exist. If even one element is missing or the iterable is empty, all() will return False. It’s a powerful way to ensure that all items in a group meet specific criteria. It takes an iterable as input and always returns a boolean value of True/False as output.

Syntax of Python all()

result = all(iterable)

Explanation

  • result: Variable stores the result of the all() function, which will be True or False.
  • all(): Built-in function that checks if all elements in the iterable are True.
  • iterable: Collection of items (e.g., list, tuple, string) you want to check. It is the input for all() function.

Example of Python all()

my_list = [True, True, True]
result = all(my_list)
print(result)

Explanation

  • my_list = [True, True, True]: Creates a list named my_list containing only True values.
  • result = all(my_list): The all() function checks if all elements in my_list are True and stores the result in result.
  • print(result): Prints the value of result, which will be True because all elements in the list are True.

Output

True


Checking if All Elements in a List Are True with Python all()

Python all() is very useful when you need to verify that every single item in a list is True. The function will look at each element in the list, and if even one element is False, the entire function will return False. It will return True only if all the elements in the list are True. An empty list will make the function return True.

Syntax

result = all(list)

Explanation

  • result: Variable will store the boolean outcome, either True or False.
  • all(): Function that checks each element in the list.
  • list: List you are examining. It is used as input for all() function.

Example

my_list = [1, 2, 3, 4]
result = all(my_list)
print(result)

Explanation

  • my_list = [1, 2, 3, 4]: Creates a list named my_list containing non-zero integers.
  • result = all(my_list): The all() function checks if all elements in my_list are True and stores the result in result.
  • print(result): Prints the value of result, which is True because all non-zero integers are considered True.

Output

True


How Python all() Works for Lists?

When you use Python all() with a list, it checks each element in the list sequentially. The function will return True only if all the elements are True otherwise it will return False. The elements in the list can be of different data types like, integers, floats, booleans etc. all() function returns True for an empty list as there are no False elements present.

Syntax

result = all(list)

Explanation

  • result: Variable will store the boolean outcome, either True or False.
  • all(): Function that checks each element in the list.
  • list: List you are examining. This acts as an input for all() function.

Example 1

my_list = [1, True, "hello", 5.5]
result = all(my_list)
print(result)

Explanation

  • my_list = [1, True, "hello", 5.5]: Creates a list my_list with an integer, a boolean, a string, and a float.
  • result = all(my_list): Here, all() checks if all elements in my_list are True and result is assigned to result.
  • print(result): Prints the result which is True because all elements are considered True.

Output

True

Example 2

my_list = [1, 2, 3, 4]
result = all(my_list)
print(result)

my_list_with_zero = [1, 2, 0, 4]
result = all(my_list_with_zero)
print(result)

empty_list = []
result = all(empty_list)
print(result)

Explanation

  • For lists, all() checks each element
  • An empty list is considered True
  • If any element in the list evaluates to False, then all() returns False

Output

True
False
True


Checking if All Elements in a List Meet a Condition with Python all()

Python all() can be used along with a condition to verify that each list element satisfies that condition. This is achieved using a generator expression inside the all() function. The generator expression applies the condition to each element in the list and all() then checks if the condition is True for all of them. If all elements satisfy the condition, all() will return True or False.

Syntax

result = all(condition for item in list)

Explanation

  • result: Variable will store the boolean result of the check.
  • all(): Function checks if the condition is True for all items.
  • condition for item in list: Generator expression that evaluates the condition for each item in the list. This is used as input for all() function.

Example 1

my_list = [10, 20, 30, 40]
result = all(x > 5 for x in my_list)
print(result)

Explanation

  • my_list = [10, 20, 30, 40]: Creates a list named my_list containing integers.
  • result = all(x > 5 for x in my_list): The all() function checks, using a generator expression, if all numbers in my_list are greater than 5.
  • print(result): Prints the value of result, which is True because all numbers in the list are greater than 5.

Output

True

Example 2

numbers = [2, 4, 6, 0]
result = all(num % 2 == 0 for num in numbers)
print(result)

Explanation

  • numbers = [2, 4, 6, 0]: Creates a list of numbers.
  • result = all(num % 2 == 0 for num in numbers): Uses a generator expression within all(). It checks if each num is even. Since 0 is present (and 0 % 2 == 0 is True), result is False.
  • print(result): Prints the outcome (False) to the console.

Output

False


How Python all() Works for Strings?

When used with strings, Python all() treats each character in the string as an individual element. An empty string will result in all() returning True because no False elements are present. For a non-empty string, all() will return True because a non-empty string is always considered True in Python.

Syntax

result = all(string)

Explanation

  • result: Variable will store the result of the all() function.
  • all(): Function checks each character in the string.
  • string: String you are evaluating. This acts as input for all() function.

Example 1

my_string = "abc"
result = all(my_string)
print(result)

Explanation

  • my_string = "abc": Creates a string named my_string.
  • result = all(my_string): The all() function checks if all characters in my_string are True and stores the result in result.
  • print(result): Prints the value of result, which is True because a non-empty string is considered True.

Output

True

Example 2

my_string = "Hello World"
result = all(my_string)
print(result)

empty_string = ""
result = all(empty_string)
print(result)

Explanation

  • In the case of strings, all() considers any non-empty string as True. Even a string with just whitespace is considered True.
  • An empty string is evaluated as False.

Output

True
True


How Python all() Works with Python Dictionaries?

With dictionaries, Python all() checks the keys, not the values. It iterates through each key in the dictionary, and if all keys are considered True, then all() returns True. If even one key is False or the dictionary is empty, it returns False. Remember that in Python, non-zero numbers, non-empty strings, and True itself are considered True, while 0, empty strings, and False are considered False.

Syntax

result = all(dictionary)

Explanation

  • result: Variable stores the boolean outcome.
  • all(): Function checks the truthiness of each key in the dictionary.
  • dictionary: Dictionary you are examining. This is passed as input to all() function.

Example 1

my_dict = {1: "apple", 2: "banana", 3: "cherry"}
result = all(my_dict)
print(result)

Explanation

  • my_dict = {1: "apple", 2: "banana", 3: "cherry"}: Creates a dictionary named my_dict with non-zero integer keys.
  • result = all(my_dict): The all() function checks if all keys in my_dict are True and stores the result in result.
  • print(result): Prints the value of result, which is True because all keys (which are non-zero integers) are considered True.

Output

True

Example 2

my_dict = {'a': 1, 'b': 2, 'c': 3}
result = all(my_dict)
print(result)

empty_dict = {}
result = all(empty_dict)
print(result)

my_dict_with_false = {'a': 1, 'b': 0, 'c': 3}
result = all(my_dict_with_false)
print(result)

Explanation

  • For dictionaries, all() checks the keys, not the values
  • An empty dictionary is considered True
  • If any key in the dictionary evaluates to False, then all() returns False

Output

True
True
False


Conclusion

Python all() function verifies if all elements in an iterable meet a specific condition or are considered True. Whether you’re working with lists, strings, or dictionaries, all() provides a concise and readable way to perform these checks. Understanding how Python all works will enable you to write more efficient and expressive Python code, especially when dealing with logical operations on data collections. all() function always returns a boolean value, either True or False.


Also Read

Python any()

Python ascii()


Python Reference

python all()

Table of Contents