How to reverse the objects in a list in Python?


In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array.

In this article, we will show you how to reverse the objects/elements in a list using python. The following are the 4 different methods to accomplish this task −

  • Using built-in reverse() method

  • Using the builtin reversed() method

  • Using Slicing

  • Using For Loop, pop() and append() Functions

Assume we have taken a list containing some elements. We will return a new list after reversing all the items of an input list using different methods as specified above.

Method 1: Using built-in reverse() method

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list.

  • Reverse all the items of a list using the reverse() function by applying it to the input list.

    The reverse() method reverses the items on the list. This function accepts no arguments.

    The reverse() method returns nothing. It updates the existing list.

  • Print the result list after reversing the input list items

Example

The following program returns the result list after reversing all the items of an input list using the reverse() method −

# input list demoList = [4, 7, 2, 1, 6] # reversing the list items demoList.reverse() # printing the list after reversing list items print('The output list after reversing:', demoList)

Output

On executing, the above program will generate the following output −

The output list after reversing: [6, 1, 2, 7, 4]

Method 2: Using the builtin reversed() method

The reversed() method returns the reverse of a given sequence object in the form of a list

Syntax

reversed(sequence)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list.

  • Use the reversed() function() to reverse all the items of an input list by passing the list as an argument to it and then convert the result into a list using the list() function(The list() method in Python accepts any iterable as an argument and returns a list)

  • Print the result list after reversing the input list items.

Example

The following program returns the result list after reversing all the items of an input list using the reversed() method −

# input list demoList = [4, 7, 2, 1, 6] # reversing the list items and converting the result into # list using list() function reversedList = list(reversed(demoList)) # printing the list after reversing list items print('The output list after reversing:', reversedList)

Output

The output list after reversing: [6, 1, 2, 7, 4]

Method 3: Using Slicing

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list.

  • Use the slicing method to reverse all the items of an input list and create a variable to store it.

  • Print the result list after reversing the input list items.

Syntax

reversedList = demoList[::-1]

Here ‘::-1’ says we are traveling from the end to start one by one element in reverse order (negative slicing).

Example

The following program returns the result list after reversing all the items of an input list using the slicing −

# input list demoList = [4, 7, 2, 1, 6] # reversing the list items using slicing reversedList = demoList[::-1] # printing the list after reversing list items print('The output list after reversing:', reversedList)

Output

The output list after reversing: [6, 1, 2, 7, 4]

Method 4: Using For Loop, pop() and append() Functions

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list.

  • Create an empty list to store the list items in reverse order

  • Use the for loop, to traverse through each element of the list till the end using the len() function(The number of items in an object is returned by the len() method).

  • Remove the last list element using the pop() function(removes the last element from a list and returns it) and append it to the above result list using the append() function(adds the element to the list at the end).

  • Print the result list after reversing the input list items.

Example

The following program returns the result list after reversing all the items of an input list using the for loop, pop, and append() functions −

# input list demoList = [4, 7, 2, 1, 6] # storing the list items in reverse order reversed_list = [] # Traversing through each element of the list till the end for i in range(len(demoList)): # removing the last list element and appending it to the empty list reversed_list.append(demoList.pop()) # printing the list after reversing list items print('The output list after reversing:', reversed_list)

Output

On executing, the above program will generate the following output −

The output list after reversing: [6, 1, 2, 7, 4]

We have given a list containing some random data. Then we used another empty list to contain the original list's reversed values. Then, using the for loop, we traversed the length of the list and popped the element from the list, the pop() function will remove the element from the list from the last, and we put the popped element to the reversed list using the append function. Finally, we printed the list in reverse.

Conclusion

We learned how to reverse the list's elements/objects using four different approaches in this tutorial. We learned how to use the functions reverse() and reversed(). We identified how to use negative slicing to get all of the elements in reverse order. We also learned how to delete elements from the list by using the pop() method and how to add elements to the list by using the append() function

Updated on: 19-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements