# list re-orderng headache

Raymond Hettinger vze4rx4y at verizon.net
Mon Aug 25 04:38:32 CEST 2003

"Gordon Airport" <uce at ftc.gov> wrote in message
> I don't know why, but I am wearing myself out trying to get this
> to work with reasonable looking code. I need to take a list of even size
> (probably >= 14, but generality never hurts) and produce a code by
> looking at the items at odd indexes from the right half and the evens
> from the left. To make things worse, the left half needs to either be
> read from the center out or reversed later.
>
> For example from [0, 1, 2, 3, 4, 5, 6, 7. 8, 9, 10, 11, 12, 13, 14, 15]
> I would encode [7, 5, 3, 1] and [8, 10, 12, 14]
> and [0, 1, 2, 3, 4, 5, 6. 7, 8, 9, 10, 11, 12, 13]
> gives [5, 3, 1] and [8, 10, 12]
>
> My problem has been that with every additional 2 items you swap the odd
> and even at the middle of the list, so reading out from the center by
> two only works half the time... Maybe I want to use the extended 2.3
> index slicing too much.

>>> def f(x):
n = len(x)
return x[1:n//2:2][::-1], x[-2:-n//2-1:-2][::-1]

>>> f(range(16))
([7, 5, 3, 1], [8, 10, 12, 14])
>>> f(range(14))
([5, 3, 1], [8, 10, 12])

Raymond Hettinger