I'm using glom project.
Is there a way to convert [{"id": 1, "name": "foo"}, {"id": 2, "name": "bar"}] to {1: "foo", 2: "bar"}?
[{"id": 1, "name": "foo"}, {"id": 2, "name": "bar"}]
{1: "foo", 2: "bar"}
New glom version (19.2.0) allows to use Merge to merge two dicts.
glom
Merge
from glom import glom, T, Merge target = [{"id": 1, "name": "foo"}, {"id": 2, "name": "bar"}] spec = Merge([{T["id"]: T["name"]}]) glom(target, spec) # {1: 'foo', 2: 'bar'}
Docs: https://glom.readthedocs.io/en/latest/api.html?highlight=merge#glom.Merge
Copyright © 2021 Jogjafile Inc.
New
glom
version (19.2.0) allows to useMerge
to merge two dicts.Docs: https://glom.readthedocs.io/en/latest/api.html?highlight=merge#glom.Merge