Skip to content

Instantly share code, notes, and snippets.

@johnpaulhayes
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnpaulhayes/07593893cf4c755c4a16 to your computer and use it in GitHub Desktop.
Save johnpaulhayes/07593893cf4c755c4a16 to your computer and use it in GitHub Desktop.
Sum a value field in a dictionary
dicts_with_values = [{"value": 30}, {"value": 20}, {"value": 1}]
""" Horrible! """
result = reduce(lambda x, y: x + y, [i["value"] for i in dicts_with_values])
""" Nice, Pythonic """
result = sum([i["value"] for i in dicts_with_values])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment