0

what can I do if I want to check every item in a list without using a lot of if-statments

The code:

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

if letters == 1:
   print("same")
else:
   print("not same")

apparently what happens that I am comparing a list to an int which are completely different.

I have tried this using a for-loop but it checks every item individually and print me the result for each item individually.

The code:

a = 1
b = 1
c = 1
letters = [a , b , c]
for letter in letters:
    if letter == 1:
        print("same")
    else:
        print("Not same")

I know that it can be solved using this method:

a = 1
b = 1
c = 1


if a == 1 and b == 1 and c == 1:
    print("same")
else:
    print("not same")

But it is very time consuming especially when you have got a lot of items.

Tiger Lion
  • 35
  • 1
  • 5
  • 1
    `if all(lt == 1 for lt in letters)` – azro Jul 16 '21 at 21:23
  • 1
    You need to use a loop, but not the way you are using it now... think about it... how would you prevent it from printing "Not same" or "same" for *every item*? You should really try to figure this out yourself – juanpa.arrivillaga Jul 16 '21 at 21:24

0 Answers0