Question: I have a dictionary and a set. I want to return the key associated with the minimum value if that key is not already in the set without creating a second dictionary used to delete items that are already in the set.

Example: I have dictionary

```
d = {1:10, 2:20, 3:30, 4:40}

```

and a set

```
s = set([1,2])

```

It should return 3

Answer: 
Use the set operations to subtract the set `s` from the set of keys of `d`, then use `min` with `key=d.get` to get the remaining key with the smallest value:

```
d = {1:10, 2:20, 3:30, 4:40}
s = set([1,2])

print min(set(d) - s, key=d.get)

```

prints `3`

If the result must be `None` when no key was found use:

```
q = set(d) - s
print min(q, key=d.get) if q else None

```

Rather than the `ValueError` from `min`.
Set operations work nicely here.

```
d = {1:10, 2:20, 3:30, 4:40}
s = set([1,2])

def find_min(d, s):
    keys = set(d)
    difference = keys-s
    return min(difference) if difference else None

>>> print find_min(d, s)
3

```

alternatively, you could have `difference = filter(lambda e: e not in s, d)`