1

I have a few lists called find. I want to know if these find are part of full_list. Lists find 1-4 are part of full_list while lists find 5-7 are not.

The example below returns "Yes".

find1 = [[1, 1]]
find2 = [[1, 1], [2, 2]]
find3 = [[1, 1], [3, 3]]
find4 = [[4, 4], [2, 2], [3, 3]]

find5 = [[1, 0]]
find6 = [[1, 1], [2, 0]]
find7 = [[1, 1], [3, 0]]

full_list = [[1, 1], [2, 2], [3, 3], [4, 4]]

if find2[0] in full_list and find2[1] in full_list:
    print("Yes")
else:
    print("No")

Because len(find2) != len(find4), the current if statement is very clumsy and almost useless.

How to make this work in a more universal way?

3 Answers 3

3

You could use all() with a generator which returns a True if everything is a truthy or False:

if all(x in full_list for x in find2):
    print("Yes")
else:
    print("No")

# => prints Yes

This one is generic, just need to change find2 to any list you need to check with full_list.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

set1 = set(find1)
full_set = set(full_list)
if set1.issubset(full_set):
    print("Yes")
else:
    print("No")

1 Comment

We have to convert list to tuples first, as they aren't hashable.
1

You could set up a function to process all of these, all the mentioned methods will work, just displaying other options, you could use filter and compare the lens as well

def is_in_full(k):
    l = list(filter(lambda x: x in k, full_list))
    if len(l) == len(k):
        return 'Yes'
    else:
        return 'No'

print(is_in_full(find1))
# Yes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.