Skip to content
Advertisement

Transform object into keymap structure?

I’m looking for a quick way to transform an object into a structure like this in Python:

obj = [{"id": "a"}, {"id": "b"}, {"id": "c"}]

Into:

mapobj = {"a": {"id": "a"}, "b": {"id": "b"}, "c": {"id": "c"}}

In javascript we can use losdash’s _.mapkeys()for this.

Advertisement

Answer

mapobj = {dct["id"]: dct for dct in obj}
Advertisement