TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

List comprehensions in Python

2 pointsby helwralmost 14 years ago

1 comment

beaumartinezalmost 14 years ago
As he says towards the beginning of his post, <i>Python 2.4 even adds a new kind of comprehension based on generators</i>; unless you actually <i>need</i> a list, you should use <i>generator expressions</i> as they use less memory (namely, as generators, individual items are lazy-loaded; in lists they are <i>all</i> loaded into memory)[1][2].<p>(That said, he also says towards the end: <i>Don't email me asking for me to optimize your code for you, [...] that's what the Python profiler is for</i>.)<p>[1] <a href="http://docs.python.org/reference/expressions.html#generator-expressions" rel="nofollow">http://docs.python.org/reference/expressions.html#generator-...</a> [2] <a href="http://www.python.org/dev/peps/pep-0289/" rel="nofollow">http://www.python.org/dev/peps/pep-0289/</a>