Test if all elements of a python list are False

PythonList

Python Problem Overview


How to return False if all elements are in a list are False?

The given list is:

data = [False, False, False]

Python Solutions


Solution 1 - Python

Using any:

>>> data = [False, False, False]
>>> not any(data)
True

any will return True if there's any truth value in the iterable.

Solution 2 - Python

Basically there are two functions that deal with an iterable and return True or False depending on which boolean values elements of the sequence evaluate to.

  1. all(iterable) returns True if all elements of the iterable are considered as true values (like reduce(operator.and_, iterable)).

  2. any(iterable) returns True if at least one element of the iterable is a true value (again, using functional stuff, reduce(operator.or_, iterable)).

Using the all function, you can map operator.not_ over your list or just build a new sequence with negated values and check that all the elements of the new sequence are true:

>>> all(not element for element in data)

With the any function, you can check that at least one element is true and then negate the result since you need to return False if there's a true element:

>>> not any(data)

According to De Morgan's law, these two variants will return the same result, but I would prefer the last one (which uses any) because it is shorter, more readable (and can be intuitively understood as "there isn't a true value in data") and more efficient (since you don't build any extra sequences).

Solution 3 - Python

Here is another approach using the generator expression:

data = [False, False, False]
try:
    out = next(elt for elt in data if elt) # holds the value of first non-empty element
except StopIteration:
    print("all elements are empty")

data = [[], [], [1], [2]]
try:
    out = next(elt for elt in data if elt) # [1]
except StopIteration:
    print("all elements are empty")

Solution 4 - Python

We all know that False is also considered 0, So if sum of all elements is 0, which means all elements within list are False.
But since you want: > to return 'false' because all elements are 'false'

To do that use negation operator not or !.

data = [False, False, False]
print(sum(data)!=0) #False

Solution 5 - Python

True and False are the Boolean representations of 0 and 1. True = 1 and Flase = 0

data = [False, False, False]    
if sum(data)== 0:
   return False
if sum(data) == len(data):
   return True

sum(data) represents the addition of 1 and 0 with respective values of True(1) and False(0) in a list.

In the case of all False sum is 0, and in the case of all True sum is equal to the length of the list. any sum value other than 0 & 1 means not all is False or True.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRomanView Question on Stackoverflow
Solution 1 - PythonfalsetruView Answer on Stackoverflow
Solution 2 - PythonAlexey OrlenkoView Answer on Stackoverflow
Solution 3 - PythonKrishna ChaurasiaView Answer on Stackoverflow
Solution 4 - PythonTAHER El MehdiView Answer on Stackoverflow
Solution 5 - PythonAbhijit ManepatilView Answer on Stackoverflow