3 ms·
List comprehensions in Python
- beaumartinez 15y agoAs he says towards the beginning of his post, Python 2.4 even adds a new kind of comprehension based on generators; unless you actually need a list, you should use generator expressions as they use less memory (namely, as generators, individual items are lazy-loaded; in lists they are all loaded into memory)[1][2]. (That said, he also says towards the end: Don't email me asking for me to optimize your code for you, [...] that's what the Python profiler is for.) [1] http://docs.python.org/reference/expressions.html#generator-expressions http://docs.python.org/reference/expressions.html#generator-... [2] http://www.python.org/dev/peps/pep-0289/ http://www.python.org/dev/peps/pep-0289/