-1

I have a long list of values and want a list comprehension to evaluate to True (and print "True" only once if any value in the list is the integer 1).

I can print "True" for each instance a 1 is found but cannot see how to just have it return a single True.

Code

a = [0,0,1,1,0,1]

b = [print("True") for i in a if i == 1]
print('\n')
#c = [print("True") if any i in a is True] # doesn't work, syntax error



d = [print("TRUE") if any(i == 1)]
Windy71
  • 851
  • 1
  • 9
  • 30

2 Answers2

1

Did you mean to convert the resulting list to bool()?

a = [0,0,1,1,0,1]
b = bool([i for i in a if i == 1])
print(b)
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • Thats what I meant quamrana, I will rewrite the question to reflect that in case it helps someone else. Much appreciated. – Windy71 Jun 16 '21 at 08:00
1

if your list only contains zeros and ones you could just print(any(a)) otherwise you could do this

a = [0,0,1,0,2,0]
b =[x==1 for x in a]
print(any(b))

returns True

Valentin C
  • 176
  • 7