Il Wed, 18 Mar 2009 23:31:09 -0200, Gabriel Genellina ha scritto: > En Wed, 18 Mar 2009 18:49:19 -0200, mattia <ger...@gmail.com> escribió: >> Il Wed, 18 Mar 2009 13:20:14 -0700, Aahz ha scritto: >>> In article <49c1562a$0$1115$4fafb...@reader1.news.tin.it>, mattia >>> <ger...@gmail.com> wrote: >>>> >>>> Yeah, and I believe that we can say the same for: 1 - t = [x*2 for x >>>> in range(10)] >>>> 2 - t = list(x*2 for x in range(10)) >>>> or not? >>> The latter requires generator expressions, which means it only works >>> with Python 2.4 or higher. Personally, I think that if the intent is >>> to create a list you should just use a listcomp instead of list() on a >>> genexp. >> Ok, so list(x*2 for x in range(10)) actually means: list((x*2 for x in >> range(10)) --> so a generator is created and then the list function is >> called? > > Exactly. The (()) were considered redundant in this case. > >> Also, dealing with memory, [...] will be deleted when the reference >> will be no longer needed and with list(...)... well, I don't know? I'm >> new to python so sorry if this are nonsense. > > I don't completely understand your question, but *any* object is > destroyed when the last reference to it is gone (in CPython, the > destructor is called at the very moment the reference count reaches > zero; other implementations may behave differently).
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? -- http://mail.python.org/mailman/listinfo/python-list