Remove Entire List from List

Viewed 58

I have a list of lists as shown below:

[['Person1', 'www.google.co.uk', '1'], ['Person2', 'www.amazon.co.uk', '2'],['Person3', 'www.google.co.uk', '2']]

In my other list I have:

[['Person1', 'www.google.co.uk'], ['Person3', 'www.ebay.co.uk']]

My desired output would be:

[['Person2', 'www.amazon.co.uk', '2'],['Person3', 'www.google.co.uk', '2']]

So if it already exists in the other list then remove that list from the original list. Is something like this possible if I do not store the 3rd value of the lists?

Code Tried:

ComparedList = [v for v in NewList if v not in List]
2 Answers

You just need the right value to use to search List:

NewList = [['Person1', 'www.google.co.uk', '1'], ['Person2', 'www.amazon.co.uk', '2'],['Person3', 'www.google.co.uk', '2']]

List = [['Person1', 'www.google.co.uk'], ['Person3', 'www.ebay.co.uk']]

ComparedList = [v for v in NewList if v[:2] not in List]
print(ComparedList)

Output as expected.

[i for i in my_list if i[:2] not in my_other_list]

Should do the trick.

Related