On Mon, Mar 24, 2014 at 3:14 PM, Rustom Mody <rustompm...@gmail.com> wrote: > Neat! So I play around... Change it to > [(x,y) for x in range(1,10000) for y in range(1,10000)] > and I dont have an answer but a thrashing machine!! (*)
Yes, because you used square brackets, which means that the list has to be fully realized. As you comment, range changed from returning a list to returning an iterable, and this action is similarly cheap: >>> ((x,y) for x in range(1,10000) for y in range(1,10000)) <generator object <genexpr> at 0x7f53ed61b360> You can take a few elements from that cheaply: >>> [next(_),next(_),next(_)] [(1, 1), (1, 2), (1, 3)] If you like thinking in "lazy lists", you can probably think just as easily with generators; you can't pull up arbitrary elements from it, or query its length, but for many purposes a generator will do. ChrisA -- https://mail.python.org/mailman/listinfo/python-list