3

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]
martineau
  • 119,623
  • 25
  • 170
  • 301
Will
  • 255
  • 3
  • 14
  • 1
    Can you supply some code of what you've tried so far? – will-hedges Apr 20 '21 at 15:50
  • 1
    The main change from the linked duplicate is that you will need to check whether item[:2] for each item in list 1 is in the second list. – Jvinniec Apr 20 '21 at 15:59
  • Consider also renaming your "List" variable to something different since "list" is a protected word in python and it could spare you some troubleshooting as well as help you having better python-style code. – MarAja Apr 20 '21 at 16:04

2 Answers2

1

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.

quamrana
  • 37,849
  • 12
  • 53
  • 71
0
[i for i in my_list if i[:2] not in my_other_list]

Should do the trick.

MarAja
  • 1,547
  • 4
  • 20
  • 36