Grant Edwards wrote: > On 2007-09-15, Erik Jones <[EMAIL PROTECTED]> wrote: > >>>> print ''.join([str(i) for i in [1,2,3]]) >>> It's better to use generator comprehension instead of LC: >>> >>> ",".join(str(i) for i in [1, 2, 3]) >> Why is that? That entire expression must be evaluated to obtain the >> result, so what is the advantage of using a generator comprehension >> v. a list comprehension? > > The generator avoids creating the intermediate list -- it > generates the intermediate values on the fly. For short > sequences it probably doesn't matter much. For a very long > list it's probably noticable.
Not true. str.join() creates a list from the iterator if it is not already a list or a tuple. In Objects/stringobject.c, look at string_join(); it calls PySequence_Fast() on the argument. Looking in Objects/abstract.c, we see that PySequence_Fast() short-circuits lists and tuples but builds a full list from the iterable otherwise. map() seems to reliably be the fastest option, and list comprehensions seem to slightly edge out generator comprehensions if you do the timings. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list