Reduce Sum in Python
Level 3106 keystrokes
This uses reduce() to accumulate values by applying a function cumulatively to list items.
Python code
# reduce() lives in the functools module1from functools import reduce2 # The numbers to add up3nums = [1, 2, 3, 4, 5]# a is the running total, b the next item: (((1+2)+3)+4)+5 = 154total = reduce(lambda a, b: a + b, nums)# Prints 155print(total)Output
15
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Python code and how often they appear.
,× 6(× 2)× 2=× 2:× 1[× 1]× 1+× 1