rlbond
rlbond

Reputation: 67847

Add tuple to list of tuples in Python

I am new to python and don't know the best way to do this.

I have a list of tuples which represent points and another list which represents offsets. I need a set of all the combinations that this forms. Here's some code:

offsets = [( 0, 0),( 0,-1),( 0, 1),( 1, 0),(-1, 0)]
points = [( 1, 5),( 3, 3),( 8, 7)]

So my set of combined points should be

[( 1, 5),( 1, 4),( 1, 6),( 2, 5),( 0, 5),
 ( 3, 3),( 3, 2),( 3, 4),( 4, 3),( 2, 3),
 ( 8, 7),( 8, 6),( 8, 8),( 9, 7),( 7, 7)]

I'm not able to use NumPy or any other libraries.

Upvotes: 18

Views: 83010

Answers (4)

Alex Martelli
Alex Martelli

Reputation: 882741

Personally, I like Alok's answer. However, for fans of itertools, the itertools-based equivalent (in Python 2.6 and later) is:

import itertools as it
ps = [(x+dx, y+dy) for (x, y), (dx, dy) in it.product(points, offsets)]

However, in this case the itertools solution is not faster than the simple one (it's actually a tad slower because it needs to unpack each x, y repeatedly for every offset, while Alok's simple approach unpacks each x, y but once). Still, itertools.product is an excellent alternative to nested loops in other cases, so, it's worth knowing about it!-)

Upvotes: 8

Alok Singhal
Alok Singhal

Reputation: 96251

result = [(x+dx, y+dy) for x,y in points for dx,dy in offsets]

For more, see list comprehensions.

Upvotes: 33

John Machin
John Machin

Reputation: 83032

If you don't care about duplicates in the result:

result = []
for ox, oy in offsets:
    for px, py in points:
        result.append((px + ox, py + oy))

If you do care about duplicates in the result:

result = set()
for ox, oy in offsets:
    for px, py in points:
        result.add((px + ox, py + oy))

Upvotes: 5

Joel Bender
Joel Bender

Reputation: 885

Pretty simple:

>>> rslt = []
>>> for x, y in points:
...     for dx, dy in offsets:
...         rslt.append( (x+dx, y+dy) )
... 
>>> rslt
[(1, 5), (1, 4), (1, 6), (2, 5), (0, 5), (3, 3), (3, 2), (3, 4), (4, 3), (2, 3), (8, 7), (8, 6), (8, 8), (9, 7), (7, 7)]

Cycle through the points and the offsets, then build new tuples of adding the offsets to the points.

Upvotes: 15

Related Questions