Archive
Posts Tagged ‘reverse a slice’
Reverse a part (slice) of a list
March 21, 2012
Leave a comment
Problem
You have a list and you want to reverse a part (slice) of it.
Solution
Let our list be [1,2,9,6,5] and say we want to reverse its end from element 9, i.e. we want to get [1,2,5,6,9].
a = [1,2,9,6,5] i = 2 # reverse from this index position j = 4 # reverse until this index position (included) a[i:j+1] = reversed(a[i:j+1]) print a # [1, 2, 5, 6, 9]
Categories: python
reverse, reverse a slice
