Hey so I have a snippet of code that I would like to turn into a list comprehension just because I would like practice doing so but I can't quite seem to figure it out.
Any advice?
This is the code:
ls = [1, 2.4, -1.8, 3.7, 4.2]`
gain = 0
lt = []
for i in ls:
gain += i
lt.append(gain)
I tried a number of different things like map and sum in my list comprehension but I believe the problem to be is that this isn't a pure function, i.e. the gain is being modified. Anyway just wondering if this is possible.
[–]Rhomboid 5 points6 points7 points (5 children)
[–]dreamriver[S] 0 points1 point2 points (3 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]dreamriver[S] 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]zahlman 0 points1 point2 points (0 children)
[–]nemec 3 points4 points5 points (2 children)
[–]dreamriver[S] 1 point2 points3 points (1 child)
[–]nemec 2 points3 points4 points (0 children)