Python - emulate sum() using list comprehension -
is possible emulate sum() using list comprehension ?
for example - need calculate product of elements in list :
list = [1, 2, 3] product = [magic_here in list] #product expected 6
code doing same :
def product_of(input): result = 1 in input: result *= return result
no; list comprehension produces list long input. need 1 of python's other functional tools (specifically reduce()
in case) fold sequence single value.
Comments
Post a Comment