How do I reverse a list in Python?

97 views

How do I reverse a list in Python?

How do I reverse a list in Python?

solveurit24@gmail.com Changed status to publish February 13, 2025
0

There are multiple ways to reverse a list. Here are two common methods:

  1. In-place reversal:

my_list = [1, 2, 3, 4]
my_list.reverse()
print(my_list)  # Output: [4, 3, 2, 1]

  1. Create a new reversed list:

my_list = [1, 2, 3, 4]
reversed_list = my_list[::-1]
print(reversed_list)  # Output: [4, 3, 2, 1]

solveurit24@gmail.com Changed status to publish February 13, 2025
0