I have an input dictionary, I want to compare keys with another dictionary and if the key in the lookup dict gets hit, I want it to run a function as the value in the lookup dict.
The function in the look up dict will return a key, value pair that needs to be "updated" into a new dict. So here is an example:
out = {getattr(self,self.LOOKUPDICT[k])({k:query[k]}) for k in query.keys() if k not in self.exclusions}
The result of
getattr(self,self.LOOKUPDICT[k])({k:query[k]})
Is a dictionary that looks like this:
{'new_key':'new_val'}
In the comprehension I am getting
TypeError: unhashable type: 'dict'
Because it is expecting a key value pair.
Now I can just run a regular loop and an if statement, but I've become kind of adverse to those. Any workaround here?
TLDR: Dictionary comprehensions, how to insert a dict like this:
{{'k':'v'} for k in blahblah}
and make k the key and v a value. Note that the dict is returned from a function that gets ran in the expression.
A dictionary comprehension expects separate
key
andvalue
expressions:but if your function produces both, you'd be better of using a
dict()
function with loop:because that function takes a sequence of tuples each containing a key-value pair.
Note that the
.keys()
call is not needed here; iteration directly overquery
is enough to yield keys. You could use set intersections here instead; thedict.keys()
object is a dictionary view that itself acts as a set:where the
query.keys() - self.exclusions
expression produces a set of keys that are inquery
but not in the iterable (set, dict, list, tuple, etc.)self.exclusions
.If the
self.LOOKUPDICT
values map to attributes onself
, it could be useful to directly reach into the instance dictionary with: