Is it possible to emulate something like sum() using list comprehension ?
For example - I need to calculate the product of all elements in a list :
l
Starting Python 3.8, and the introduction of assignment expressions (PEP 572) (:= operator), we can use and increment a variable within a list comprehension and thus reduce a list to the sum of its elements:
total = 0
[total := total + x for x in [1, 2, 3, 4, 5]]
# 15
This:
total to 0total is incremented by the current looped item (total := total + x) via an assignment expression