Here's the question you clicked on:
AppylPye
Mechanical MOOC OCW Exercise 2.7 Working With Lists Solution: cumulative_sum.py Here's a solution to the cumulative_sum exercise - 2 versions - the second function using a list comprehension. Does anyone else have a solution to post, so that we can compare different approaches? http://ideone.com/x38UtA
Thanks Appyl - I'm happy (and surprised) to report that my solution, with the exception of my variable names, is virtually the same as yours. I'm glad you explained that what the second function was doing involved list comprehensions - I'll have to pick that apart slowly.
In this statement ... return [sum(number_list[:i + 1]) for i in range(len(number_list))] ... each index in "number_list" is considered, by virtue of the fact that "i" takes on values successively matching each index in the list, as specified by the "range" function. As this occurs, the "sum" function returns the sum of all values in the slice of "number_list" that extends from the beginning of the list, up to, and including the value at the index corresponding to the current value of "i".
Thanks for that explanation. I made it to the List comprehensions section and also worked through this http://www.codecademy.com/courses/python-beginner-en-5oLLh-btkJb -- I was surprised at what I could accomplish, and now I would almost call them "fun"!
Yes, that Non-track Codecademy List comprehensions course by Iryna Cherniavska is really great - "fun" - yes :)