0

I'd like to check if all elements within my list are equal to a certain value (that being, '1'). For example:

check_a = False
check_b = False
check_c = False

a = [0, 0, 0, 0, 0]
b = [0, 1, 1, 0, 0]
c = [1, 1, 1, 1, 1]

if all in a == 1:
    check_a = True

elif all in b == 1:
    check_b = True

elif all in c == 1:
    check_c = True
  

1 Answers1

-3

You are quite close. It looks like you can just use all(elem == 1 for elem in list) in this case:

check_a = check_b = check_c = check_d = False

a = [0, 0, 0, 0, 0]
b = [0, 1, 1, 0, 0]
c = [1, 1, 1, 1, 1]
# notice we want this to result in `False`
#  c = [1, True, 1, 1, 1]
d = [1, 2, 3, 1, 5]


def check_all(L: list):
    return all(x == 1 and x is not True for x in L)


if check_all(a):
    check_a = True

if check_all(b):
    check_b = True

if check_all(c):
    check_c = True

if check_all(d):
    check_d = True


# False False True False
print(check_a, check_b, check_c, check_d)
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53