I suppose it could be the opposite: a list could be created under the hood to pass it to sum(). So when a list is declared straightforward - it can be more efficient. I noticed that usually list comprehension shows faster result than generator.
But even more faster and memory efficient can be the for loop since in this case there is no additional iterable would be created at all.
Let's hope beginners won't take this as best practice for coding. While clever and compact, such code is very hard to read in a production environment.
Nice comparison.
You can skip the "[ ]" in the sum, as it forces the creation of a list. Without it, sum will work on a generator, which will consum less memory.
because anyone, including those that have not yet solved the kata, can see the comments
why use spoiler flags in the comment section of the answers?
Mine isnt a one liner and runs about 2200 ms on avg.
I suppose it could be the opposite: a list could be created under the hood to pass it to sum(). So when a list is declared straightforward - it can be more efficient. I noticed that usually list comprehension shows faster result than generator.
But even more faster and memory efficient can be the for loop since in this case there is no additional iterable would be created at all.
Mine isn't a one-liner and on average runs around 2800ms
This comment is hidden because it contains spoiler information about the solution
💕
Let's hope beginners won't take this as best practice for coding. While clever and compact, such code is very hard to read in a production environment.
Nice comparison.
You can skip the "[ ]" in the sum, as it forces the creation of a list. Without it, sum will work on a generator, which will consum less memory.
Thanks a lot, I'm new to Python and don't have quite the logic and syntax in mind yet, so pocket tutorials like this are a big help