Python

Python All Function

Python provides a wide range of built-in methods that enable us to swiftly implement algorithms that would otherwise require a lot more time to develop from scratch. One of the factors that make Python such a potent programming language is how easily these functions can be accessed.

In this post, we’ll examine the Python method all() and see how it treats different structures of the data and types of data. The only thing limiting the use-cases of the built-in all() method is a developer’s creativity. In this post, I’ll show some simple usage, a few advanced applications, and hopefully demonstrate why every Python coder should get familiarized with the all() function.

The all() function in Python is used to verify the accuracy of each member of a collection. Collections, python dictionaries, generators, sets, and even strings are among the various iterable types available in Python. The all() function can be utilized with any of these data types. It can be contrasted to the more semantic check of each item in a list to see if it’s true. A list, dictionary, or another iterable object may be passed as an argument to the all() function in Python.

Syntax of all() function in python language:

Let’s begin by discussing the all() function’s syntax.

Syntax: all(iterable)

  • bool(‘i’) is returned as True if it is true for each value of ‘i’ in the iterable.
  • If the iterable is void, this function returns True.

The all() method outputs true if every value in the provided iterable is true. Alternatively, it returns false. Additionally, if we have the empty iterable, this function returns true.

Example 1: Using all() function with python list

To demonstrate how the all() method functions, consider these brief examples of the python list:

List = [8, 4, 2]

print(all(List))

List = [1, 1, False]

print(all(List))

List = [0, 9, 2, 5, False]

print(all(List))

List = []

print(all(List))

Here, we have created our first list with the name List1 which has some numerical values. Like all the elements, what we have inserted in the list are true. So, it generates the true boolean value from this particular list. For this, we have called python’s all() function inside our print statement. It will generate the boolean result of the given list.

Then, we have taken another list which is named List2. So, the all() function generates the false boolean value. The third list which is named List3 has also one false value and it also generates the false value. This means that the all() function will return false if the list contains at least one false value. The last list List4 is a little unclear because it provides True with an empty list because none of the list’s items are False.

All the boolean value returned from the all() function with the above-specified list is as follows:

Example 2: Using all() function with python sets:

So far, the only instance of the all() method that has taken a list as an argument is in this article (except for empty tuples). A tuple, dictionary, or set are just a few examples of iterables that you can employ. The python sets also act like lists when the all() function is used with them. Examples include:

set1 = {2, 5, 10}

print(all(set1))

set2 = {9, False, 9}

print(all(set2))

set3 = {9, 6, 3, 0, False}

print(all(set3))

set4 = {}

print(all(set4))

Here, we have defined some Python sets. The first set is represented by set1 and has all the true values. The second set is defined as set2 where the middle value is false and the remaining two values are true. The third set is assigned a name set3 which has the last value false and the last set set4 has an empty value. We have invoked all() functions each set inside the print statement for displaying the boolean values.

In the output, we have boolean values returned by the all function for each given set respectively.

Example 3: Using all() function with python tuples:

The behavior of the all() function, when used with Python tuples,  is identical to the example above. Consider the case below.

tuple1 = (7, 3, 1)

print(all(tuple1))

tuple2 = (False, 0, False)

print(all(tuple2))

tuple3 = (6, 4, 0, 8, False)

print(all(tuple3))

tuple4 = ()

print(all(tuple4))

Here, four tuples with unique values have been defined. Some tuples contain all true values and some contain at least one false value. The last tuple consists of the empty value. The all() function takes all the specified tuples as an argument and returns the boolean result corresponding to each tuple.

The following boolean results are generated by the all() function for the given tuples:

Example 4: Using all() function with python dictionaries:

Data structures that connect keys and values include dictionaries, which are also known as mappings. As a result, when the all() function is applied to a dictionary, the output is determined solely by the keys. Consider this:

dic1 = {1: "Python", 2: "Py"}

print(all(dic1))

dic2 = {0: "Python", False: "Py"}

print(all(dic2))

dic3 = {0: "Spyder", 1: "Python", 2: "Py"}

print(all(dic3))

dic4 = {}

print(all(dic4))

Here, we have specified four dictionaries. The first dictionary we used is with boolean True values for all of the keys. The function yields False when the key is changed to 0 or an empty string. As all the keys are boolean True, even if we change a value to a boolean False empty list, we still obtain True as the output. The only thing the all() method utilizes are the keys, as seen by this.

The following boolean results are obtained:

Example 5: Using all() function with python strings:

As a collection of distinct characters, strings are iterable. When a string is passed to the all() function, it behaves just like a list. Consider this:

str1 = "My string example!"

print(all(str1))

str2 = "9999"

print(all(str2))

str3 = ""

print(all(str3))

It should be noted that the all() function generally returns True when given a string. This is because there cannot possibly be an empty character in the string. Since we already know that an empty list yields True, an empty string acts in the same way.

Conclusion

In this post, we first examined all() function’s fundamental Python usage. The representation of various data types and data structures as booleans was then discussed. The all() function was then used in a few cases with a variety of sequences as parameters. We examined the handling of texts and integers by the all() function. One of the language’s most helpful features, in our opinion, is the all() function in Python. In our experience, its batch-test truthiness functionality is useful regularly.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content