Help doing it the "python way"

Paul Rubin no.email at nospam.invalid
Thu May 24 16:55:04 EDT 2012


Scott Siegler <scott.siegler at gmail.com> writes:
> is there a way to do something like: 
>    [(x,y-1), (x,y+1) for zzz in coord_list]
> or something along those lines?

You should read the docs of the itertools module on general principles,
since they are very enlightening in many ways.  Your particular problem
can be handled with itertools.chain:
 
    from itertools import chain
    new_list = chain( ((x,y-1), (x,y+1)) for x,y in coord_list )

You can alternatively write an iterative loop:

   def gen_expand(coord_list):
     for x,y in coord_list:
       yield (x-1,y)
       yield (x+1, y)

   new_list = list(gen_expand(coord_list))



More information about the Python-list mailing list