En Thu, 19 Mar 2009 08:06:35 -0200, mattia <ger...@gmail.com> escribió:

OK, understood. Now, as a general rule, is it correct to say:
- use generator expression when I just need to iterate over the list or
call a function that involve an iterator (e.g. sum) and get the result,
so the list is not necessary anymore
- use list comprehensions when I actually have to use the list (e.g. I
need to swap some values or I need to use sorted() etc.)
Am I right?

Yes, at least that's how I use them: a generator expression when the elements are consumed one by one in the same moment, and a list comprehension when I actually want the list as a whole.

(Note that sorted() has to return a new list anyway, its argument may be a gen.expr. but sorted() starts by making a list out of it; you may be thinking of the list.sort() method)

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to