Program to reverse a list by list slicing in Python

List slicing in Python provides a simple and efficient way to reverse a list using the slice notation [::-1]. This approach creates a new list with elements in reverse order without modifying the original list.

Understanding List Slicing Syntax

List slicing takes three parameters separated by colons: [start:end:step]

  • start − Starting index (default: 0)
  • end − Ending index (default: length of list)
  • step − Step size (default: 1)

For reversing, we use [::-1] where:

  • Empty start and end means include entire list
  • -1 step means move backwards through the list

Example

Here's how to reverse a list using slicing ?

def solve(nums):
    return nums[::-1]

nums = [5, 7, 6, 4, 6, 9, 3, 6, 2]
reversed_nums = solve(nums)

print("Original list:", nums)
print("Reversed list:", reversed_nums)
Original list: [5, 7, 6, 4, 6, 9, 3, 6, 2]
Reversed list: [2, 6, 3, 9, 6, 4, 6, 7, 5]

Direct Slicing Without Function

You can also reverse a list directly without creating a function ?

nums = [5, 7, 6, 4, 6, 9, 3, 6, 2]
reversed_nums = nums[::-1]

print("Original:", nums)
print("Reversed:", reversed_nums)
print("Original unchanged:", nums)
Original: [5, 7, 6, 4, 6, 9, 3, 6, 2]
Reversed: [2, 6, 3, 9, 6, 4, 6, 7, 5]
Original unchanged: [5, 7, 6, 4, 6, 9, 3, 6, 2]

Key Points

  • List slicing with [::-1] creates a new list
  • The original list remains unchanged
  • This method works with any sequence type (strings, tuples)
  • Time complexity is O(n) where n is the length of the list

Conclusion

List slicing with [::-1] is the most Pythonic way to reverse a list. It's concise, readable, and creates a new reversed list without modifying the original.

Updated on: 2026-03-26T15:35:29+05:30

318 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements