Python List reverse() Method with Examples

In the previous article, we have discussed Python List remove() Method with Examples
List in Python:

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.

List reverse() Method in Python:

The reverse() method reverses the list’s elements.

Syntax:

list.reverse()

Parameters: This function has no parameters.

Return Value:

The reverse() method produces no output. It is used to update the existing list.

Examples:

Example1:

Input:

Given List = ['welcome', 'to', 'Python', 'programs']

Output:

The given original list: ['welcome', 'to', 'Python', 'programs']
The given list after reversing :  ['programs', 'Python', 'to', 'welcome']

Example2:

Input:

Given List = [10, 20, 30, 40, 50]

Output:

The given original list: [10, 20, 30, 40, 50]
The given list after reversing :  [50, 40, 30, 20, 10]

List reverse() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Print the given original list.
  • Apply reverse() function to the given list that reverses all the elements of the given list.
  • Print the list after reversing all its elements.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["welcome", "to", "Python", "programs"]
# Print the given original list.
print("The given original list:", gvn_lst)
# Apply reverse() function to the given list that reverses all the elements of
# the given list.
gvn_lst.reverse()
# Print the list after reversing all its elements.
print("The given list after reversing : ", gvn_lst)

Output:

The given original list: ['welcome', 'to', 'Python', 'programs']
The given list after reversing :  ['programs', 'Python', 'to', 'welcome']

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Print the given original list.
  • Apply reverse() function to the given list that reverses all the elements of the given list.
  • Print the list after reversing all its elements.
  • The Exit of Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Print the given original list.
print("The given original list:", gvn_lst)
# Apply reverse() function to the given list that reverses all the elements of
# the given list.
gvn_lst.reverse()
# Print the list after reversing all its elements.
print("The given list after reversing : ", gvn_lst)

Output:

Enter some random List Elements separated by spaces = 10 20 30 40 50
The given original list: [10, 20, 30, 40, 50]
The given list after reversing : [50, 40, 30, 20, 10]

Reversing a list Using Slicing Operator

Approach:

  • Give the list as static input and store it in a variable.
  • Print the given original list.
  • Reverse the elements of the given list using the slicing operator and store it in another variable.
  • Print the list after reversing all its elements.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ['good', 'morning', 'btechgeeks', 25, 35]
# Print the given original list.
print("The Given original list is :", gvn_lst)
# Reverse the elements of the given list using the slicing operator and
# store it in another variable.
reversee_lst = gvn_lst[::-1]
# Print the list after reversing all its elements.
print("The given list after reversing : ", reversee_lst)

Output:

The Given original list is : ['good', 'morning', 'btechgeeks', 25, 35]
The given list after reversing :  [35, 25, 'btechgeeks', 'morning', 'good']

Covered all examples on python concepts and missed checking out the Python List Method Examples then go with the available link & gain more knowledge about List method concepts in python.