0

I want to reverse the last x elements in a list in python.

For example - Reverse the last 3 elements.


list = [1,2,3,4,5,6,7,8]

# Output needed = [1,2,3,4,5,8,7,6]

dheeraj
  • 368
  • 2
  • 8

1 Answers1

0

Used the following command to do it -

a = [1,2,3,4,5,6,7,8]
count = 3 #Number of elements you want reverse from last.
a = a[:-count] + a[-count:][::-1]
print(a) #[1,2,3,4,5,8,7,6]
dheeraj
  • 368
  • 2
  • 8