On Thu, May 24, 2012 at 5:12 PM, Emile van Sebille <em...@fenx.com> wrote: > On 5/24/2012 2:30 PM Paul Rubin said... > >> Paul Rubin<no.email@nospam.invalid> writes: >>> >>> new_list = chain( ((x,y-1), (x,y+1)) for x,y in coord_list ) >> >> >> Sorry: >> >> new_list = list(chain( ((x,y-1), (x,y+1)) for x,y in coord_list)) > > > >>>> from itertools import chain >>>> coord_list = zip(range(20,30),range(30,40)) >>>> a = [((x,y-1),(x,y+1)) for x,y in coord_list] >>>> b = list(chain(((x,y-1),(x,y+1)) for x,y in coord_list)) >>>> a == b > True >>>> > > So, why use chain? Is it a premature optimization? Similar to the practice > of using "".join(targets) vs targeta+targetb+...+targetn?
Paul's code is incorrect. It should use chain.from_iterable in place of chain. The difference then is that it creates a single list of coordinates, rather than a list of pairs of coordinates. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list