Posted on

As we can recall, to get every other element, we can use

>>> seq = [7, 2, 3, 6, 3, 5, 6, 0, 1]
>>> seq[::2]
[7, 3, 3, 6, 1]
seq[::2]

where 2 is the step, and with it, only the elements indexed with 0, 2, 4, 6… are taken.

The trick here is to apply a negative step.

>>> seq = [7, 2, 3, 6, 3, 5, 6, 0, 1]
>>> seq[::-1]
[1, 0, 6, 5, 3, 6, 3, 2, 7]

References

  1. Python for Data Analysis, 2n Edition, Wes McKinney, O’Reilly 2017

Leave a Reply

Your email address will not be published. Required fields are marked *