Last active
August 29, 2015 14:19
-
-
Save johnpaulhayes/07593893cf4c755c4a16 to your computer and use it in GitHub Desktop.
Sum a value field in a dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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