简体   繁体   中英

Python all items in list of lists true

Made a table of True & False, and all I'd like to do is check if they are all True. Used all() successfully before, but for some reason with the below I fail miserably.

data = [[False, False, False], 
[False, False, False], 
[True, True, True], 
[True, True, True]]

print(all(data))
>>> True

Why is this happening?

all does not check the bools in each sublist. Each of the non-empty lists are all truthy.

To check that all the items in all sublists are True , you should do:

all(x for lst in data for x in lst) # -> False

You can pass each sublist to the all function within all using a generator:

print(all(all(i) for i in data))

Output:

False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM