| by Arround The Web | No comments

Reverse order of a List in Python

The “Lists” are one of Python’s built-in data types. It can be utilized to store data and multiple items/elements in a single variable appropriately. Python provides multiple inbuilt functions that are used to perform various operations with the list. One of the common tasks/operations in Python is reversing a list.

This Python post will present multiple ways to reverse the order of a list using numerous examples.

How to Reverse the Order of a Python List?

To reverse the order of the Python list, the following methods/approaches are utilized in Python:

Method 1: Reverse Python List Order by Utilizing the “reverse()” Method

In Python, the “reverse()” method reverses the list of elements without accepting any argument.

Syntax

list.reverse()

 

Example

The below example code is utilized to reverse the order of the Python list:

list1 = [12, 33, 45, 57]
print('Given List:', list1)
list1.reverse()
print('Reversed List:', list1)

 

In the above code, the “list.reverse()” method reverses the order of the input list by using the dot syntax notation.

Output

The given list elements have been reversed successfully.

Method 2: Reverse Python List Order by Utilizing “List Comprehension”

The “List Comprehension” approach creates a list by utilizing the existing elements of the list. This approach is used along with the “range()” function to reverse the order of the Python list.

Example

Let’s overview the below example code:

list1 = [12, 33, 45, 57]
print('Given List:', list1)
reversed_list = [list1[i] for i in range(len(list1) - 1, -1, -1)]
print('Reversed List:', reversed_list)

 

According to the above code:

  • The list “list1” is initialized and displayed.
  • The “range()” function is utilized inside the “List Comprehension” approach that starts from the list end and moves towards the start of the list.
  • In the “List Comprehension”, the “for” loop loops through the “range()” function and reverses a list.

Output

As analyzed, the given list elements have been reversed successfully.

Method 3: Reverse Python List Order by Utilizing the “slice()” Function

The “slice()” function takes the specified start, end, and step parameters and retrieves the slice object in Python.

Syntax

slice(start, end, step)

 

Here, the “start”, “end”, and “step” parameters are used to specify the beginning, ending, and step size of the slicing.

Example

The below example code is employed to reverse the Python list order:

list1 = [12, 33, 45, 57]
print('Given List:', list1)
reversed_list = list1[slice(None, None, -1)]
print('Reversed List:', reversed_list)

 

Based on the above code:

  • The list “list1” is initialized and displayed in the console.
  • The “slice()” function is utilized to reverse the list using three parameters: “None”, “None”, and “-1”, respectively.
  • The first, “None”, means that we want to start from the beginning of the list, and the second “None”, implies that we want to stop at the end of the list. The “-1” means that we want to step back through the list.

Output

Method 4: Reverse Python List Order by Utilizing the “for” Loop

In Python, the “for” loop can also be utilized along with the empty list to reverse the order of a Python list.

Example

Let’s go through the following example code:

list1 = [12, 33, 45, 57]
print('Given List:', list1)
reversed_list = []
for i in list1:
    reversed_list = [i] + reversed_list
print('Reversed List:', reversed_list)

 

Here:

  • The initialized and empty lists are initialized, respectively.
  • The order of the Python list is reversed using the “for” loop to iterate over the list and concatenate each element of the list into an empty list.

Output

Conclusion

The “reverse()” method, “List Comprehension”, “slice()” function, or the “for” loop are utilized to reverse the order of a Python list. The “reverse()” method, however, is recommended as it is a simple and straightforward way to reverse the order of the Python list. This guide presented various ways to reverse the order of a list in Python using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply