Skip to content

Instantly share code, notes, and snippets.

@riywo
Created March 25, 2013 23:00
Show Gist options
  • Save riywo/5241652 to your computer and use it in GitHub Desktop.
Save riywo/5241652 to your computer and use it in GitHub Desktop.
>>> dict(map(lambda x: [x["k"], x], [{"k":"1", "v":1}, {"k":"2","v":2}]))
{'1': {'k': '1', 'v': 1}, '2': {'k': '2', 'v': 2}}
@methane
Copy link

methane commented Mar 26, 2013

map + lambda よりもリスト内包の方が良いです

>>> dict([(x.pop("k"), x) for x in [{"k":"1", "v":1}, {"k":"2","v":2}]])
{'1': {'v': 1}, '2': {'v': 2}}

@methane
Copy link

methane commented Mar 26, 2013

Python 2.7 以降なら辞書内包表記も使えます

>>> {x.pop("k"): x for x in [{"k":"1", "v":1}, {"k":"2","v":2}]}
{'1': {'v': 1}, '2': {'v': 2}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment