Skip to content

Instantly share code, notes, and snippets.

@formido
Last active November 13, 2023 09:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save formido/7d40e4791391020f2c02 to your computer and use it in GitHub Desktop.
Save formido/7d40e4791391020f2c02 to your computer and use it in GitHub Desktop.
map list of dicts with lambda in python

I like to use higher order functions like map, I like to use lambdas, and I prefer to keep my intermediate data representations in lists of dictionaries. Here's a way to map a list of dictionaries concisely:

>>> favorites = [{'name':'home', 'visited':5}, {'name':'work', 'visited':10}]
>>> map(lambda x: dict(x, **{'visited': x['visited']+1}), favorites)
[{'visited': 6, 'name': 'home'}, {'visited': 11, 'name': 'work'}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment