2
list1 = ['Gryffindor', 'Ravenclaw', 'Hufflepuff', 'Slytherin']
list2 = ['Gryffindor', 'Ravenclaw']

checkif = item in List2 for item in List1

if check is True:
    print("The list {} contains all elements of the list {}".format(List1, List2))

Why is this damn thing not working? Also is checkif = item in list2 for item in list1 a list comprehension or what?

Someone please correct my code, thanks.

Shah Jacob
  • 137
  • 1
  • 9
  • 1
    it's not valid syntax. Even if you *correct* it to make it a list comprehension or generator expression, it wouldn't evaluate to a bool object, rather, a list object or a generator object. – juanpa.arrivillaga Apr 22 '21 at 03:07
  • You have a couple of typos. For `checkif`, it should be `list1` and `list2`, not `List1` and `List2`. Also, it should be `if checkif is True`, not `if check is True`. – M-Chen-3 Apr 22 '21 at 03:11

2 Answers2

5

I think you want all:

list1 = ['Gryffindor', 'Ravenclaw', 'Hufflepuff', 'Slytherin']
list2 = ['Gryffindor', 'Ravenclaw']

checkif = all(item in list1 for item in list2)

Also, you need to swap list1 and list1 to get the result you describe in the print line.

Z4-tier
  • 7,287
  • 3
  • 26
  • 42
  • @ShahJacob ????? Try setting `list2 = ['Gryffindor', 'Ravenclaw', 'foobar']`, then `all(item in list1 for item in list2)` will be false. – Z4-tier Apr 22 '21 at 22:47
  • actually i'm an idiot sorry. i am so lost right now i really need your help. can you please explain the syntax (item in list1 for item in list2), it literally is the most confusing syntax ever. i just cannot figure out what it means. i know 'in' is a keywords that checks if an element is in a sequence or whatever, but what does ```item in list1``` AND THEN ```for item in list2``` mean? is this some type of weird list comprehenion? how can you write ```for item in list``` without a ```:``` and for loop body, i've never seen that type of for loop before. can you please explain the syntax – Shah Jacob Apr 23 '21 at 02:44
  • You will have luck if you look for *python iterators* and *python list comprehensions* (although this syntax doesn't use a list comprehension, they are closely related). The syntax `item in list1 for item in list2` works like this: *for each item in list2 check if the item is in list1*. That's it. `item in list1` returns a boolean (True of False) for each iteration. wrapping it with `all` will produce a single True response only in the case where `item in list1` is True for **every** item that is checked. In other words, every item in `list2` is in `list1`. Otherwise, `all` will return False. – Z4-tier Apr 23 '21 at 02:56
  • i still do not understand at all... if this isn't a python list comprehension, then what is this syntax/concept? a list comprehension is the only situation where you can write a ```for``` loop without a body. what do you mean if i look for python iterators, i already know what an iterators is, they're just lists or strings or dictionaries. can you please explain the syntax... i don't get what ```item in list` for item in list2``` even means, i've never seen that syntax before. can you please explain the syntasx – Shah Jacob Apr 23 '21 at 03:20
  • i just don't get the syntax. i know item in list1 means item in the bigger list, but what does ```for iterm in list2``` mean? why do you randomly just put a for loop statement/iterator thing right after (what i assume is a conditional) item in list1? why don't you write something like for i in list2: for i in list 1: if i... something, man i don't know even know. you can write a nested for loop that does this right, could you show me what that nested for loop would look like after explaining your syntax again – Shah Jacob Apr 23 '21 at 03:26
  • man i just don't understand ``` all(item in list1 for item in list2)```. you're using the ```in``` keyword in the BIGGER list and then iterating through the smaller list? why don't you iterate through the big list first instead and use the ```in``` one the second list? please explain – Shah Jacob Apr 23 '21 at 03:29
4

You can use sets here and specifically check the relationship whether one of your sets is a subset of the other:

set(list2).issubset(list1)   # True

To use this the first object must be a set hence set(list2) but the second can be any iterable. One caveat here is that since we're comparing sets, it will only check for unique elements, i.e. it will not care about repeated values.

NotAName
  • 3,821
  • 2
  • 29
  • 44