Searching for something thats not in a list

Basically I’m trying to search a list from a list (not sure if they are actually lists or dictionary, but I’ve included the exact input) and if item from list 1 is not in list 2 I want to remove it from list 1, what ever I do all I get is what’s in the list and not what’s not in the list.

import datetime

listone = {}
listtwo = []

listtwo.append(["","test 1"])
listtwo.append(["","test 6"])
listtwo.append(["","test 5"])
listtwo.append(["","test 4"])
listtwo.append(["","test 3"])
listtwo.append(["","test 2"])
listtwo.append(["", "test 1"])

temp = listone.copy()
for key, value in temp.items():
    for item in listtwo:
        if item[0] != key:
            listone.pop(item[0], None)

Now I’m only getting back somesite 1 and 3 in listone after the code has ran, which it should be somesite 2 in listone and 1 and 3 should be removed. Can someone point out where I’m going wrong please?


You need to test if an element is not in the whole list of filtered elements before deciding to remove it. There’s a simpler solution: first, extract the items that you want to use for filtering. I’ll use a set for efficiency and a generator expression for extracting the URLs:

urls = set(url for url, text in listtwo)

Then, create a new dictionary without the elements that you want to filter; here I’m using a dictionary comprehension:

listone = {k: v for k, v in listone.items() if k in urls}

The result will be as expected:

=> {'': datetime.datetime(2021, 4, 15, 13, 38, 20, 388197)}

By the way, listone is actually a dictionary, not a list.