How to check if a list is inside the other?

1

My code:

n = [int(input()) for c in range(0, 5)]
lista = list(range(0, 10))
if n not in lista:
    print('1')
else:
    print('2')

I type 1, 2, 3, 4, 5 and the answer is "1", the numbers 1 through 5 are within the "list" that includes the numbers 0 through 10, so I guess I am not relating list with the other correctly. I would like to know how to check if the values in the first list are within the second, if in case the input is the example I used.

    
asked by anonymous 23.12.2017 / 19:50

1 answer

2

You use the intersection method and check for common values in your set or list. Basically, it checks if the elements of n belongs to lista .

See:

n = [int(input()) for c in range(0, 5)]

lista = list(range(0, 10))

if set(n).intersection(lista):
  print('1')
else:
  print('2')

You can query other methods that can help you with list operations.

See more about the Intersection .

    
23.12.2017 / 20:23