A list comprehension to do this?...

Andrew Koenig ark at research.att.com
Wed Sep 18 17:08:31 EDT 2002


joe> Is there a list comprehension or other 'cleaner' or 'prettier' way 
joe> to do the following:

>>>> xx = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>>> ww = []
>>>> for i in range(0, len(xx), 2):
joe> 	ww.append((xx[i+1],xx[i]))

How about

        [(xx[i+1], xx[i]) for i in range(0, len(xx), 2)]

?
-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark



More information about the Python-list mailing list