Reverse the list in python

In this tutorial we will be focusing on how to reverse the python list. We will be trying out different ways to reverse the elements of the list in python one of them being to use the List reverse() method.

  • Reversing a list in python using list slicing
  • Reversing the list using slice function in python
  • Reverse a list in python with For loop and range function
  • Reversing a list using list reversed built in method in python
  • Reverse() and Reversed() method of list reverse

 

Syntax

The basic syntax for reverse() function in python list:

list.reverse()

Parameters:  

  • reverse() Method doesn’t take any arguments

 

 

 

Reverse the elements of list in python using simple reverse() function :

First let’s create the simple list as shown below and we will using list.reverse() as shown below in order to reverse the elements of the list


# Simple Reverse Function

fruits = ['Apple', 'Strawberry','Cherry','Mango']

fruits.reverse()

print(fruits)

output:

[‘Mango’, ‘Cherry’, ‘Strawberry’, ‘Apple’]

 

 

 

Reverse the elements of list in python using list slicing:

Syntax

  • reversed_list = list[start:stop:step]
  • slice(start, end, step)

Parameters:  

  • start: An integer number specifying at which position to start the slicing. Default is 0
  • end: An integer number specifying at which position to end the slicing
  • step: An integer number specifying the step of the slicing. Default is 1

 

We will be using the list slicing method to reverse the list as shown below. The start and stop parameters are null and step -1 which will reverse the given list.

## Reversing a list using list slicing

# Syntax: reversed_list = list[start:stop:step]

fruits = ['Apple', 'Strawberry','Cherry','Mango']

reversed_list = fruits[::-1]

print(reversed_list)

so, the output will be,

output:

[‘Mango’, ‘Cherry’, ‘Strawberry’, ‘Apple’]

 

 

 

Reversing the list using slice function in python:

slice() function as shown below is used in order to reverse the elements of the list. slice function has three parameters, The start and stop parameters are null and step -1 which will reverse the given list.

#### reversing list using slice function

fruits = ['Apple', 'Strawberry','Cherry','Mango']

slicer = slice(None, None, -1)

reversed_list = fruits[slicer]

print(reversed_list)

so, the output will be,

output:

[‘Mango’, ‘Cherry’, ‘Strawberry’, ‘Apple’]

 

 

 

Reversing the list using for loop and range() function in python:

range() function as shown below is used in order to reverse the elements of the list, range() function is similar to slice function has three parameters, The start parameter is the last index and stop parameter is -1 and step parameter is -1, which will reverse the given list.


#### reversing list using slice function

fruits = ['Apple', 'Strawberry','Cherry','Mango']

last_index = len(fruits) - 1


[fruits[i] for i in range(last_index, -1, -1)]

print(reversed_list)

so, the output will be,

output:

[‘Mango’, ‘Cherry’, ‘Strawberry’, ‘Apple’]

 

 

 

Reversing the list using reversed() function in python:

We will be using the reversed() function, which will reverse the given list


### Reversing a list using list reversed built in method

fruits = ['Apple', 'Strawberry','Cherry','Mango']


# Printing Elements in Reversed Order

fruits_reverse=[]

for o in reversed(fruits):

fruits_reverse.append(o)


print (fruits_reverse)

so, the output will be,

output:

[‘Mango’, ‘Cherry’, ‘Strawberry’, ‘Apple’]