This article will explore how to reverse a list in python in 4 different ways.
Method 1: Using list slicing
Python list slicing is used to select list elements or to prepare a sub-list from a list. Its syntax is

list[start : end : step]

start is the index of start element
end is the index of end element. Element at this index is excluded.
step is the number of indexes to skip in between

list[1:6:2] will create a sub-list with elements starting at index 1 till 5 and adding 2 at every index.
So, the sub-list will have elements at index 1, 3 and 5.

All of these arguments are optional. When skipped,
start defaults to 0,
end defaults to end of list, and
step defaults to 1.

Step size -1 instructs python to start from right(back).

Thus, if both first and second arguments are skipped and third argument is negative, it means that python will traverse the list from the back till the first element and hence, will reverse the order of list.

list_numbers = [1,3,5,6,7,9]
print("Original list :", list_numbers)
reverse = list_numbers[::-1]
print("Reversed list :", reverse)

This example program prints

Original list : 1, 3, 5, 6, 7, 9
Reversed list : 9, 7, 6, 5, 3, 1

Method 2 : Using reverse function
reverse is an inbuilt python list function which reverses a list in place.
In place means it modifies the position of elements in the original list. This means that the original list is changed.
This method is beneficial in terms of memory usage.

Python docs for reverse() state,

The reverse() method modifies the sequence in place for economy of space when reversing a large sequence

If you look at the return value of reverse() method, it returns None since it modifies the list on which it is called. Example,

list_numbers = [1,3,5,6,7,9]
print("Original list :", list_numbers)
print("Reversed list :", list_numbers)

Output of above program is

Original list : 1, 3, 5, 6, 7, 9
Reversed list : 9, 7, 6, 5, 3, 1

Method 3: Using reversed function
reversed() is a python built in function which returns a reverse iterator over the list.
Reverse iterator means that if the list is iterated using this then it will return the elements in reverse order as shown below.

list_numbers = [1,3,5,6,7,9]
print("Original list", list_numbers)
reverse_iterator = reversed(list_numbers)
print("Reversed list: ", end= "")
# iterate list
for element in reverse_iterator:
    print(element, end=" ")

Reversed list is iterated using a for in loop.
Output of this program is

Original list : 1, 3, 5, 6, 7, 9
Reversed list : 9, 7, 6, 5, 3, 1

Above example iterates over the reversed list, but if you want a new list with reversed contents, then create a list using its constructor supplying it the iterator returned by reversed() function as shown below.

reverse_iterator = reversed(list_numbers)
# create a llst from reversed function
reverse_list = list(reverse_iterator)

Method 4: Iterating by index backwards
This is a traditional method that involves iterating a python list using for loop and is based on following steps.

  1. Initialize an empty list to hold reversed elements.
  2. Iterate the list to be reversed backwards.
  3. For iterating a list backwards, follow below steps
    1. Initialize a variable to point to the last list element index, that is, equal to the (length of list – 1).
    2. Iterate a while loop from the current value of variable in above step till it reaches 0.
    3. In every iteration, access the list element at index equal to loop counter. Also, decrease Step i variable  by 1.
      This way the list will be traversed backwards.
  4. Append this list element to the list initialized in Step 1.

At the end of the loop, the list in Step 1 will have elements in reverse order.
Example program written as per these steps is given below.

list_numbers = [1,3,5,6,7,9]
# initialize empty list
reversed_list = []
print("Original list", list_numbers)
# calculate list size
list_size = len(list_numbers)
# variable pointing to the last list element
counter = list_size -1
# iterate till it becomes 0
while counter >= 0:
    # add list element to new list
    # decrease loop variable
    counter -= 1

Output of above code is

Original list : 1, 3, 5, 6, 7, 9
Reversed list : 9, 7, 6, 5, 3, 1

Hit the clap if the article was helpful.

Liked the article ? Spread the word...

Leave a Reply