On 24 May 2012 21:22, Scott Siegler <scott.sieg...@gmail.com> wrote:
> Hello,
>
> I am an experienced programmer but a beginner to python.  As such, I can 
> figure out a way to code most algorithms using more "C" style syntax.
>
> I am doing something now that I am sure is a more python way but i can't 
> quite get it right.  I was hoping someone might help.
>
> So I have a list of grid coordinates (x, y).  From that list, I want to 
> create a new list that for each coordinate, I add the coordinate just above 
> and just below (x,y+1) and (x,y-1)
>
> right now I am using a for loop to go through all the coordinates and then 
> separate append statements to add the top and bottom.
>
> is there a way to do something like: [(x,y-1), (x,y+1) for zzz in coord_list] 
> or something along those lines?

AFAICS nobody's suggested yet the simple yet effective:

    new_list = [(x, y + i) for x, y in coord_list for i in (-1, 1)]

IMHO these list comprehensions are often overlooked too quickly in
favour of itertools (in this case chain.from_iterable).

-- 
Arnaud
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to