There isn't a slice notation for this, here is how I would probably do it (this is with Python 2.x, Python 3.x code is included at the end):
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [x for s in reversed(zip(*[iter(lst)]*3)) for x in s]
[7, 8, 9, 4, 5, 6, 1, 2, 3]
This method for clustering elements into n
length groups comes straight from the zip
docs, so the approach here is to create your groups of 3, reverse the resulting list, and then use a list comprehension for flattening.
Or with the itertools module, using the grouper
recipe and chain.from_iterable
:
from itertools import izip_longest, chain
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
result = list(chain.from_iterable(reversed(list(grouper(lst, 3)))))
Python 3 versions:
For the first approach you will need to convert the generator returned by zip
to a list, otherwise reversed
will fail:
>>> [x for s in reversed(list(zip(*[iter(lst)]*3))) for x in s]
[7, 8, 9, 4, 5, 6, 1, 2, 3]
And in the itertools approach you just need to replace izip_longest
with zip_longest
in both the import statement and inside of the grouper
function.