Python all() Function

Python’s built-in all(x) function takes one iterable as an argument x such as a list, tuple, or dictionary. It returns True if all iterable elements evaluate to True using implicit Boolean conversion, otherwise it returns False. If the iterable is empty, all() returns True because the condition is satisfied for all elements.

Argumentx -> x1, x2, ..., xnIterable such as a list, tuple, or dictionary
Return Valuebool(x1) and bool(x2) ... and bool(xn) Converts all elements to the Boolean type and returns True if all elements evaluate to True using the bool() conversion function.

Interactive Code Shell

Consider the following interactive code snippet:

Exercise: Add another string value to the list so that the all() function returns False.

Hint: Only one string value evaluates to False.


But before we move on, I’m excited to present you my new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners

Example all() for Lists

The following code shows you how to use the all() function on different lists—can you figure out the individual list elements that make the function all() return False?

# Boolean list with False value
print(all([True, False, True, True]))
# False


# Boolean list without False value
print(all([True, True]))
# True


# Integer list with 0 value
print(all([1, 2, -1, 0]))
# False


# Integer list without 0 value
print(all([1, 2, -1]))
# True


# List of Lists with empty inner list
print(all([[], [1, 2, 3]]))
# False


# List of Lists without empty inner list
print(all([[1, 2, 3], [4, 5]]))
# True


# Empty List
print(all([]))
# True

Examples for Tuple

If you use the all() function on tuples, it’ll return a Boolean value that indicates whether all tuple elements evaluate to True.

print(all((1, 2, 3)))
# True

print(all((True, True, 2==2)))
# True

print(all((True, 3, 1!=1)))
# False

Examples for Dicts

The all() function on dictionaries checks for the iterable of keys (not values) whether all elements evaluate to True. If this is the case, the return value is True, otherwise it’s False.

d = {'Alice': 10000,
     'Bob': 5000,
     'Carl': 0}

print(all(d))
# True

d[''] = 100000


print(all(d))
# False

Implementation

According to the official Python documentation, the all() function is semantically equivalent to the following code snippet:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

So, it goes over all elements in the iterable and uses the element as an if condition to check whether it evaluates to True or False. As soon as one False element is detected, it aborts the loop and returns False. This is an optimization called short circuiting and it means that only the first False value is evaluated!

Python all() Function with For Loop

You can also dynamically create an iterable using a generator expression and pass it into the all() function. This may be called an “all() function with a for loop”.

print(all(x**2 == 16 for x in range(10)))
# False

You use the condition x**2 == 16 which holds only for x=4. As you apply this expression for all x values from 0 to 9 (included) by using the range() function, it mostly returns False. Due to short circuiting, the all() function returns False after evaluating the first element x=0.

Summary

Python’s built-in all(x) function takes one iterable as an argument x such as a list, tuple, or dictionary.

It returns True if all iterable elements evaluate to True using implicit Boolean conversion, otherwise it returns False. I

f the iterable is empty, all([]) returns True because the condition is satisfied for all elements.

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

[ajax_load_more post_type=”post” category=”coding-business,python” max_pages=”5″]