Is there a way to map a list onto a dictionary? What I want to do is give it a function that will return the name of a key, and the value will be the original value. For example;
somefunction(lambda a: a[0], ["hello", "world"])
=> {"h":"hello", "w":"world"}
(This isn’t a specific example that I want to do, I want a generic function like map() that can do this)
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:
Output:
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.