Map list onto dictionary
In Python 3 you can use this dictionary comprehension syntax:
def foo(somelist): return {x[0]:x for x in somelist}
I don't think a standard function exists that does exactly that, but it's very easy to construct one using the dict builtin and a comprehension:
def somefunction(keyFunction, values): return dict((keyFunction(v), v) for v in values)print somefunction(lambda a: a[0], ["hello", "world"])
Output:
{'h': 'hello', 'w': 'world'}
But coming up with a good name for this function is more difficult than implementing it. I'll leave that as an exercise for the reader.
If I understand your question correctly, I believe you can accomplish this with a combination of map
, zip
, and the dict
constructor:
def dictMap(f, xs) : return dict(zip(map(f, xs), xs)
And a saner implementation :
def dictMap(f, xs) : return dict((f(i), i) for i in xs)