How to Reverse a List in Python: Learn Python List Reverse () Method - NareshIT
Naresh i Technologies
Only Institute to offer the 'Most Comprehensive eLearning Platform to suit the self-learning needs of all CMS and LMS
Introduction:
1.?????As we know that Python Reversing a list is a way that takes up more memory compared to an in-place reversal because it creates a (shallow) copy of the list.
2.?????In general, the reversing a List?In-Place With the?list.?reverse() Method.
3.?????Every?list in Python?has a built-in?reverse() method?which is allowed to call to?reverse?the contents of the?list?object in place.
4.?????Reversing?the?list?in place means won't create a new?list?and copy the existing elements to it in?reverse?order.
Here I am going to discuss some of the important approaches using which the reversal of the list in Python can be get done.
1.?????Reversing a list with a list.reverse() method
2.?????Using the Slicing Trick to Reverse a Python List, and
3.?????Creating Reverse Iterator with the reversed() Built-In?Function
Reversing a list with a list.reverse() method:
As we have already discussed earlier in the above segment Every list in Python has a built-in reverse() method, so if it is needed then we can call this method to reverse the contents of the list object in place. Reversing a list in place means it won’t create and copy the existing elements to a new list. Instead, it directly modifies the original list object.
Let us consider the following example below. Here I am trying to explain the technique in a simpler way using the Python console-based example.
Example:
>>>mylist = [1, 2, 3, 4, 5]
>>>mylist[1, 2, 3, 4, 5]
>>>mylist.reverse()
None
>>>mylist
[5, 4, 3, 2, 1]
Note:
1.?????From the above example it is clear that when we are calling?the reverse()?method then it returned ‘None’ but, modified the original list object.
2.?????When we are using the reverse()?method then it is basically used to modify the sequence of the element present in the list.
3.?????But it should be get noted that it does not return reversed list values as an output but when we are going to print or display the list then it gets printed out.
Using the Slicing Trick to Reverse a Python List:
Slicing in Python is another feature using which we can reverse the element of the list. If we need to apply this technique, then we have to use the following technique to make it possible.
Let us consider the following example as
reverse_list(list):
reversed_list = list[::-1]
>>>mylist=[1, 2, 3, 4, 5]
>>>mylist[::-1]
[5, 4, 3, 2, 1]
Note:
1.?????Here when we are going to use this technique then the structure of the list is get replicated but the elements are not. Hence, the elements are not duplicated thus saving space.
2.?????As you know that the list in Python is mutable so when we are going to apply this technique to the elements contained in a list then the object is getting modified. Since the objects are get modified so they will be get reflected in other copies as well.
3.?????The Slicing technique is fast and reliable for the developer. But sometimes it becomes difficult to understand when the decreasing readability of code is supposed to be get done.
领英推荐
Creating Reverse Iterator with the reversed() Built-In?Function:
It is another important technique in Python using which we can reverse the list element. When we are going to use the reversed() function then it will return an iterator using which if we need then we can be able to access elements in reverse order.
But it should be get noted that If we are going to access a single individual element of the list in the reverse order you can use this?function. It does not reverse a list in a place neither it creates a copy.
Let us consider the following example below.
>>>mylist=[1,2,3,4,5]
>>>foriteminreversed(mylist):
...print(item)
5
4
3
2
1
>>>mylist
>>>[1,2,3,4,5]
Note:
1.?????In the above example it is clear that when we are going to use this function then it returns elements of the list in reverse order using the iterator pattern.
2.?????Another way of getting a reverse list using a list constructor.
To explain the concept of list constructor let us consider the following example below.
>>>mylist=[1,2,3,4,5]
>>>list(reversed(mylist))
[5,4,3,2,1]
3.?????Here in the above program I’m calling the?list()?constructor on the result of the?reversed()?function.
4.?????When we are using the list constructor it is used to built-in keeps iterating until the (reverse) iterator is exhausted.
5.?????It is basically used to keep and puts all the elements which are get fetched from the iterator during the execution and put into a new list object.
6.?????And as a result a reversed shallow?copy?of the original list is get reformed and displayed.
7.?????It is another convenient approach to reverse the list element in Python.
Scope @ NareshIT:
1.?????At Naresh IT you will get a good Experienced faculty who will guide you, mentor you and nurture you to achieve your dream goal.
2.?????Here you will get good hand on practice in terms of the practical industry-oriented environment which will definitely help you a lot to shape your future.
3.?????During the designing process of the application, we will let you know about the other aspect of the application too.
4.?????Our Expert trainer will let you know about every ins and out’s of the problem scenario.
Achieving your dream goal is our motto. Our excellent team is working restlessly for our students to click their target. So, believe in us and our advice, and we assured you about your sure success.?
?You can contact us anytime for your?Python Training?online as well as call us directly, or you can give us a missed call. And one of our customer care representatives will contact you asap.
Follow us for More Updates:?https://bit.ly/NITLinkedIN??