-1

I was working with python and wrote down some this sort of code. According to me it should be false but it is showing true.

list1=[1,2,3,4]
list2=[2,3,1]
list3=[5,6,7]
if 1 in list1 and list3:
    print(True)
else:
    print(False)
khelwood
  • 55,782
  • 14
  • 81
  • 108

1 Answers1

1

it should be

list1=[1,2,3,4]
list2=[2,3,1]
list3=[5,6,7]
if 1 in list1 and 1 in list3:
    print(True)
else:
    print(False)

that's the pythonic way of checking,

if 1 in list1 and list3 means that 1 in list1 which is True ,and list3 is the second part which will always be true until the list is not empty thus it becomes if True and True so it returns True hope you understood :D

Btw there are more ways of checking if a particular item belongs to multiple lists

list1=[1,2,3,4]
list2=[2,3,1]
list3=[5,6,7]
if all(1 in lis for lis in [list1,list3]):
    print(True)
else:
    print(False)
Oreo
  • 104
  • 5