egeberk
egeberk

Reputation: 13

controlling whether a list have all element of a list that contains sets in python

I am trying to write a function to find whether a list that contains sets are elements of another list. For instance:

list1 = [{'a', 'b'}, {'a', 'c'}, {'d', 'a'}, {'c', 'b'}, {'d', 'c'}, {'e', 'c'}, {'e', 'd'}]
and
list2 = [{'a', 'c'}, {'a', 'b'}, {'c', 'b'}]

as you can see, all elements of list2 can be found in list1. However, my code always produce false. How can I handle this problem? My code can be seen below.

check = all(item in list2 for item in list1)
print(check)

Upvotes: 1

Views: 130

Answers (1)

user_na
user_na

Reputation: 2273

You check if all elements of list1 are in list2, which is not the case, therefore you get False. I think you want to switch list1 and list2 in your check:

list1 =  [{'a', 'b'}, {'a', 'c'}, {'d', 'a'}, {'c', 'b'}, {'d', 'c'}, {'e', 'c'}, {'e', 'd'}]
list2 = [{'a', 'c'}, {'a', 'b'}, {'c', 'b'}]
check = all([item in list1 for item in list2])
print(check)

Result is True

Upvotes: 1

Related Questions