Mike Tibb - 3 months ago 18

Python 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`