Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 2012-02-08 04:02:41 -0500

DSM gravatar image

There are several ways to do this. Which one I'd use would depend more upon the context, because I'd probably write the one which generalized most naturally.

First, explicitly (useful if we're sticking with 2D or if I might be interested in other transformations):

>>> L = [(2,3), (4,5), (6,7)]
>>> M = [(y,x) for x,y in L]
>>> M
[(3, 2), (5, 4), (7, 6)]

or maybe using slice notation (seq[start:stop:step]):

>>> M = [v[::-1] for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]

Or, using words instead:

>>> M = [tuple(reversed(v)) for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
>>> M = list(tuple(reversed(v)) for v in L)
>>> M
[(3, 2), (5, 4), (7, 6)]

And if the objects are really vectors, then maybe I'd use a rotation matrix. All depends, but I have to admit if there were only two things to swap I'd reach for the first method.