Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a list contains a boolean value

Tags:

python

list

This is a part of my code:

for d in errors[:]:
    if False in d:
        Ic.append(current_mA2)

And this is in errors:

[True, True, True, False, True, True, False, True,True,True, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False,False]

I just want to know if the value False is in the list.

like image 354
srky Avatar asked Apr 04 '17 14:04

srky


People also ask

How do you check if all values are true in a list?

Python all() Function The all() function returns True if all items in an iterable are true, otherwise it returns False.

How do you tell if a list is true or false in Python?

to directly check if False is in list. list implements the __contains__ magic method so you can just do that. This works for the specific case the OP presents. Be warned, though, if the list errors contains an integer 0 etc, it automatically returns true, eg., False in [0, True] returns True .

How do you see if value is in a list Python?

We can use the in-built python List method, count(), to check if the passed element exists in the List. If the passed element exists in the List, the count() method will show the number of times it occurs in the entire list. If it is a non-zero positive number, it means an element exists in the List.


3 Answers

No one suggested using all() (python doc here). All checks wether all values in a list interpret to True, meaning, if at least one of them is False, it will return False:

> a_list = [True, True, False]
> b_list = [True, True, True]
> all(a_list)
False
> all(b_list)
True
like image 171
maykmayx Avatar answered Oct 19 '22 16:10

maykmayx


Why not just:

if False in errors:
    Ic.append(current_mA2)

to directly check if False is in list. list implements the __contains__ magic method so you can just do that.

like image 7
midor Avatar answered Oct 19 '22 14:10

midor


How about

isFalseIn = False in errors

I think that works.

like image 1
user14436501 Avatar answered Oct 19 '22 16:10

user14436501