How to Reverse a List in Python | Reverse in python 5ways with Examples – My programming school


Python array representation
81 / 100

How to Reverse a List in Python | Reverse in python 5ways with Examples – My programming school

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

Reverse a List in Python

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

List reverse() methodology

Python checklist class comes with the default reverse() perform 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 each from 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 id values for enter and output lists are the identical. Hence, we are able to say that the checklist. 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 a checklist, string, tuple, 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 the 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 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 house to maintain a duplicate object and therefore, consumes a lot of reminiscence.

"""
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 enter and out lists 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]

How to Reverse a List in Python | Reverse in python 5ways with ExamplesMy programming school

For more Search in Wikipedia

How to Reverse a List in Python | Reverse in python 5ways with Examples – My programming school

How to Reverse a List in Python | Reverse in python 5ways with Examples – My programming school

Categories

You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.