How to Reverse a List in Python | Reverse in python 4 ways with Examples

This tutorial describes 4 elegant methods to reverse a list in Python. It contains strategies like a checklist.reverse(), built-in reversed() methodology, slice operator, and utilizing for loop.

Reverse a List in Python

All strategies defined right here present ready-to-use code samples. Let’s go over every one of them one after the other.

List reverse() methodology

Python checklist class comes with the default reverse() performance that inverts the order of things in the given checklist. It doesn’t create a new checklist object as a substitute straightway modifies the unique copy.

It is essentially the most really helpful approach to transpose the checklist from a pace and efficiency perspective.

"""
Function:
 
 Desc: Reverse a list in python utilizing checklist class built-in reverse() methodology
 Param: Input checklist
 Return: Modified checklist object
"""
def invertList(input_list): 
   input_list.reverse()
   return input_list
   
# Unit check 1
input_list = [11, 27, -1, -5, 4, 3] 
print("Input list[id=] items: ".format(id(input_list), input_list))

input_list = invertList(input_list)
print("Output list[id=] items: ".format(id(input_list), input_list))

After you run the above code, it outputs the next in the console:

Input checklist[id=140126896653384] gadgets: [11, 27, -1, -5, 4, 3]
Output checklist[id=140126896653384] gadgets: [3, 4, -5, -1, 27, 11]

In the above instance, you possibly can see that we’ve known as the id() perform to print the reminiscence deal with of the checklist. And you possibly can assert that the id values for entering and output lists are identical. Hence, we are able to say that the checklist. The reverse () methodology reversed the checklist in place and didn’t create a new copy.

Reversed() methodology

It is Python’s built-in methodology that reverses all kinds of sequences such as checklists, strings, tuples, and many others. Unlike the primary methodology, neither it performs an in-place reverse operation, nor does it makes a new copy of the checklist object.

Besides, it returns an iterator pointing to the last aspect of the checklist. You can iterate over it and get the gadgets in reverse order.

"""
Function:
 
 Desc: Reverse a list in Python's built-in reversed() methodology
 Param: Input checklist
 Return: Reverse iterator
"""
def invertList(input_list): 
    return reversed(input_list)

# Unit check 1
input_list = [11, 27, -1, -5, 4, 3] 
print("Input list[type=] items: ".format(sort(input_list), input_list))

end result = invertList(input_list)
out_list = [item for item in reversed(input_list)]
print("Output list[type=] items: ".format(sort(end result), out_list))

After you run the above code, it outputs the next in the console:

Input checklist[type=<class 'list'>] gadgets: [11, 27, -1, -5, 4, 3]
Output checklist[type=<class 'list_reverseiterator'>] gadgets: [3, 4, -5, -1, 27, 11]

From the end result, you possibly can assess that the kind of our enter checklist is a List whereas the one reversed() perform returned is of Reverse iterator sort.

You might like to use such a methodology when the checklist measurement is large. It would prevent some time and reminiscence.

Slice the operator to reverse a checklist

It is a wholly totally different approach to invert the checklist. No in-place modification happens, and it makes a new copy of the List. Such a methodology calls for more houses to maintain a duplicate object and therefore, consumes a lot of reminiscences.

"""
Function:
 
 Desc: Reverse list utilizing Python slice operator
 Param: Input list
 Return: New list
"""
def invertList(input_list): 
   out_list = input_list[::-1]
   return out_list
   
# Unit check 1
input_list = [11, 27, -1, -5, 4, 3] 
print("Input list[id=] items: ".format(id(input_list), input_list))

out_list = invertList(input_list)
print("Output list[id=] items: ".format(id(out_list), out_list))

After you run the above code, it outputs the next in the console:

Input checklist[id=139774228206664] gadgets: [11, 27, -1, -5, 4, 3]
Output checklist[id=139774228208840] gadgets: [3, 4, -5, -1, 27, 11]

The end result exhibits that the slice operator created a new copy as the id values of each entry and out of the list are totally different.

Using for loop to reverse

It is a customized strategy to reverse the List utilizing a for-loop assertion. This strategy modifies the unique checklist and additionally returns a checklist.

"""
Function:
 
 Desc: Reverse checklist utilizing for loop
 Param: Input checklist
 Return: Modified enter checklist
"""
def invertList(input_list): 
   for merchandise in vary(len(input_list)//2): 
      input_list[item], input_list[len(input_list)-1-item] = input_list[len(input_list)-1-item], input_list[item]
   return input_list
   
# Unit check 1
input_list = [11, 27, -1, -5, 4, 3] 
print("Input list[id=] items: ".format(id(input_list), input_list))

input_list = invertList(input_list)
print("Output list[id=] items: ".format(id(input_list), input_list))

After you run the above code, it outputs the next in the console:

Input checklist[id=140703238378568] gadgets: [11, 27, -1, -5, 4, 3]
Output checklist[id=140703238378568] gadgets: [3, 4, -5, -1, 27, 11]

Using checklist comprehension

Another custom-made strategy is to use the checklist comprehension method to reserve the checklist. See the beneath code to perceive this methodology.

"""
Function:
 
 Desc: Reverse list utilizing checklist comprehension methodology
 Param: Input checklist
 Return: Modified checklist object
"""
def invertList(input_list): 
   input_list = [input_list[n] for n in vary(len(input_list)-1,-1,-1)]
   return input_list
   
# Unit check 1
input_list = [11, 27, -1, -5, 4, 3] 
print("Input list[id=] items: ".format(id(input_list), input_list))

input_list = invertList(input_list)
print("Output list[id=] items: ".format(id(input_list), input_list))

After you run the above code, it outputs the next in the console:

Input checklist[id=140044877149256] gadgets: [11, 27, -1, -5, 4, 3]
Output checklist[id=140044877151560] gadgets: [3, 4, -5, -1, 27, 11]

For more Search Wikipedia

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.