0

List operation

What’s different x = [42, 8, 7, 1, 0, 124, 8897, 555, 3, 67, 99] num = int(input()) Between them? if num in x : print('bingo') if x in num: print('bingo')

6th Dec 2023, 3:09 PM
Umalat N
2 odpowiedzi
+ 5
Num is a single entity, while x is a list of entities. 'If x in y' will run through every entity in y, and see if x is found. In your first example (if num in x) it will check and see if the number input is in the list. In the second example, it is trying to see if the list is in your integer, which will generate a TypeError
6th Dec 2023, 3:16 PM
StuartH
StuartH - avatar
+ 1
You cannot check if a list is inside an int, but you can ask if a list is inside a list. lst_a = [1, 2, 3, 4] lst_b = [11, 12, 13, 14] lst_c = [lst_a, lst_b] if lst_a in lst_c: print(True) else: print(False) It prints True.
7th Dec 2023, 3:13 AM
Wong Hei Ming
Wong Hei Ming - avatar