Reverse a List <ONE LINER> In Python
Given a = [1,2,3,4,5]; how do you reverse it, using your own scratch algorithm.
Why not use Python slice step size to do it.
>>> a = list(range(1,6))
>>> a
[1, 2, 3, 4, 5]
>>> a[::-1]
[5, 4, 3, 2, 1]
>>> a
[1, 2, 3, 4, 5]
>>> a[::-2]
[5, 3, 1]
- Slicing on a some part, and latter on reversing the elements
>>> a
[1, 2, 3, 4, 5]
>>> a[-2:-5:-1]
[4, 3, 2]
- With string
>>> n="video" >>> n 'video' >>> n[::-1] 'oediv' >>> n 'video' >>> n[::-2] 'odv'
- Slicing on a some part, and latter on reversing the elements
>>> n
'video'
>>> n[4:1:-1]
'oed'
>>> n[-2::-1]
'ediv'
>>> n[-2:-5:-1]
'edi'
>>> n[1:4:-1]
''