swapping numeric items in a list

Boris Borcic bborcic at gmail.com
Wed Aug 23 09:09:31 EDT 2006


Jiang Nutao wrote:
> Hi,
> 
> I simplify my problem like below
> 
> To convert list
>     aa = [0x12, 0x34, 0x56, 0x78]
> into
>     [0x34, 0x12, 0x78, 0x56]
> 
> How to do it fast? My real list is huge.

Mark Rintsch's suggestion appears best if applicable, but just to cite yet other 
ways to do it :

list(aa[j+(-1)**j] for j in range(len(aa)))

or

sum(reversed(zip(*[reversed(aa)]*2)),())

or (py 2.5)

from itertools import izip
ab = iter(aa)
list((yield y) or x for x,y in izip(ab,ab))




More information about the Python-list mailing list