How to Reverse List Elements in Python

Python provides several ways to reverse the elements of the list or arrays in Python. In this tutorial, I will explain reversing a list by using list reverse(), built-in reversed, slicing approach, using for loop, and finally using pop() and append() together.

Below are the methods that are used to reverse a list in Python.

  1. Reverse List using reversed()
  2. Reverse List using list.reverse()
  3. Using for loop with list.insert()
  4. Slice to get the list in reverse
  5. Using for loop
  6. Using slice() function
  7. Using list.append() & list.pop() methods

1. Quick Examples of Reversing List in Python

Following are quick examples of how to reverse a list.


# Consider list of countries
countries=["India","China","Russia","China"]
print("Actual List: ",countries)

# Example 1: Using insert()
reversed = [] 
for i in countries:
    reversed.insert(0,i)
print("Reversed List: ",reversed)

# Example 2: Using Slicing
print("Reversed List: ",countries[::-1])

# Example 3: Using for loop
reversed = []
for i in countries:
  reversed = [i] + reversed
print("Reversed List: ",reversed)

# Example 4: Using slice()
print("Reversed List: ",countries[slice(None, None, -1)])

# Example 5: Using for loop
reversed = []
for i in range(len(countries)):
  reversed.append(countries.pop())
print("Reversed List: ",reversed)

# Example 6: Using reverse()
countries.reverse()
print("Reversed List: ",countries)

# Example 7: Using reversed()
print("Reversed List: ",list(reversed(countries)))

2. Reverse List using reversed() Method

The reversed() is a built-in function in python that will reverse any iterable like a list, tuple, dictionary etc, and returns a list_reverseiterator, To reverse the list you need to pass our list object as a parameter to this method and convert the result to a list by using the list() function.

2.1 reversed() Syntax

Let’s see how to use reversed() function.


# Syntax
list(reversed(mylist1))

Here, mylist1 is the input list.

2.2 Using reversed() to Reverse Python List

In this example, let’s create a python list with a set of countries, and let’s reverse the list using the reversed() function.

Note that this function returns the list_reverseiterator, so to get the list, it should convert back using list().


# Consider list of countries
countries=["India","China","Russia","China"]
print("Actual List: ",countries)

# Using reversed()
result = reversed(countries)
print("Return Type of reversed(): "+str(type(result)))
print("Reversed List: ",list(result))

This example yields the below output.

python reverse list

3. Reverse list using reverse() method

The list.reverse() method in python can also be used to reverse the list, it reverses the list in place meaning it updates the original list with the reversed order. It will return None. If we print the list after using this method, the reversed list is displayed.

3.1 reverse() Syntax

Let’s see how to use the reverse() method.


# Here, mylist1 is the input list.
mylist1.reverse()

3.2 reverse() Example

Let’s have a list that stores 4 countries and reverse them using reverse() method.


# Consider list of countries
countries=["India","China","Russia","China"]
print("Actual List: ",countries)

# Using reverse()
countries.reverse()
print("Reversed List: ",countries)

# Output:
# Actual List:  ['India', 'China', 'Russia', 'China']
# Reversed List:  ['China', 'Russia', 'China', 'India']

We can see that list is reversed.

4. Reverse List using insert() Method

We will use list.insert() method to insert an element at a particular position in the List. We can utilize this method with for loop to create a new list by reversing it.

First, we will use for loop to iterate each element, and inside this loop, we will insert the element at the first position. So the next element gets added again in the first position. By the end of the for loop, all elements will be added to list in reverse order.

4.1 insert() Syntax

Let’s see how to use the insert() method.


# Syntax
reversed = [] 
for iterator in mylist1:
    reversed.insert(0,iterator)

4.2 insert() Example

Let’s have a list that stores the marks of 5 students and reverse them using the insert() method. Here, first, you need to initialize the new list with no elements. We will add elements to this list one by one.


# Create list of elements
marks=[100,89,78,90,88]
print("Actual List: ",marks)

# Using insert()
reversed = [] 
for i in marks:
    reversed.insert(0,i)
print("Reversed List: ",reversed)

# Output:
# Actual List:  [100, 89, 78, 90, 88]
# Reversed List:  [88, 90, 78, 89, 100]

5. Reverse list using append() and pop() methods

Similarly, we can also use pop() to get the element from the python list and use append() to insert it into new list which ideally gives the new list in the reverse order. First, we will use for loop to iterate each element and inside this loop, we will remove the last element from the list using the pop() method and append this to the new list using append(). With this, the new list holds the elements in reverse order.

5.1 Syntax

Let’s see how to use append() and pop() methods inside the for loop


# Syntax
reversed = []
for iterator in range(len(list)):
  reversed.append(mylist1.pop())

5.2 Example

Let’s have a list that stores country names and reverse them using append() and pop() methods.


# Create a list
countries=["India","China","Russia","China"]
print("Actual List: ",countries)

# Using for loop
reversed = []
for i in range(len(countries)):
  reversed.append(countries.pop())
print("Reversed List: ",reversed)

# Output:
# Actual List:  ['India', 'China', 'Russia', 'China']
# Reversed List:  ['China', 'Russia', 'China', 'India']

We can see that the new list (reversed) holds reversed countries.

6. Reverse list with Slicing approach

Using slicing is another approach to get the list in reverse. Here, we need to specify the stop parameter while slicing list, for example [::-1], it will return the elements from the list from back to first.

6.1 Syntax

Let’s see how to perform slicing to reverse the list.


# Syntax
list[::-1]

6.2 Example

Let’s have a list that stores student marks and reverses them by slicing.


# Create list
marks=[100,89,78,90,88]
print("Actual List: ",marks)

# Using Slicing
print("Reversed List: ",marks[::-1])

# Output:
# Actual List:  [100, 89, 78, 90, 88]
# Reversed List:  [88, 90, 78, 89, 100]

We can see that the new list (reversed) holds reversed integers.

7. Reverse list using for loop

This is the naive approach where we will use for loop to iterate elements in the list and add them into a new list, So the new list contains reversed elements.

7.1 for loop Syntax

Let’s see how to reverse the list using for loop.


# Syntax
reversed = []
for iterator in mylist1:
  reversed = [iterator ] + reversed

7.2 for loop Example

Let’s have a list that stores student marks and reverse them using for loop.


# Create list
marks=[100,89,78,90,88]
print("Actual List: ",marks)

# Using for loop to reverse list
reversed = []
for i in marks:
  reversed = [i] + reversed
print("Reversed List: ",reversed)

# Output:
# Actual List:  [100, 89, 78, 90, 88]
# Reversed List:  [88, 90, 78, 89, 100]

We can see that the new list (reversed) holds reversed integers.

8. Reverse list using slice()

This approach is similar to the slicing that we discussed earlier, here, we will use the Python slice() function to reverse the elements in the given list. We need to provide the first two parameters as None and the last parameter is set to -1, which will retrieve the elements from the last.

8.1 slice() Syntax

Let’s see how to reverse the list using slice()


# Here, mylist1 is the input list.
mylist1[slice(None, None, -1)]

8.2 slice() Example

Let’s have a list that stores country names and reverses them using slice().


countries=["India","China","Russia","China"]
print("Actual List: ",countries)

# Using slice()
print("Reversed List: ",countries[slice(None, None, -1)])

# Output:
# Actual List:  ['India', 'China', 'Russia', 'China']
# Reversed List:  ['China', 'Russia', 'China', 'India']

We can see that the list is reversed.

9. Conclusion

In this article, you have learned different ways to reverse the list in Python. We have used the build-in function reversed(), reverse() method from the list, retrieve each element from the list and insert it at the first position on another list and several other examples.

Leave a Reply

You are currently viewing How to Reverse List Elements in Python