Today in Reverse A List Python tutorial we will learn how to reverse a list in python. Reversing a list is a very common operation in programming language.So let’s see how to do it in python.
- A list is mutable data structure in python.
- When you want to work with many related values then lists are great to use.
- They enable you to keep data together that belongs together, condense your code, and perform the same methods and operations on multiple values at once.
But before going further you can also check how to create a list and various operations on list from here.
Reverse A List Python Tutorial – Three Methods With Examples
Now we will see an important concept that is reversing of list.
We have a list [20,70,60,40] , and we have to reverse this list as [40,70,60,20].so let’s learn how to do this.
There are many ways to reverse a list in python, we will go trough all of them one by one, so let’s start
- Using reverse() method
- Using a list slicing trick
- Using reversed() python built-in function
Using reverse() method
The first option is python in-built reverse() method.
- Every list object in python has a method called reverse() that you can call a list object and it will reverse the list in place.
- This means we don’t need to create a new list to copy these elements to a new list but instead you can modify the original list.
- So this is very fast because it happen in place and doesn’t take up any extra memory.
- However it modifies the existing list.
So the code for doing this is –
1 2 3 4 5 6 7 8 9 |
mylist = [20,70,60,40] print("Original List : ", mylist) mylist.reverse() print("Reversed List : ", mylist) |
What We Did ?
- First of all i have created a list.
- Then printed my original list.
- And next, call reverse() method that will reverse list.
- And then simply print the reversed list.
- This is very easy to understand for someone who is new to python or comes from another language background , they are able to understand what’s going on so i really like this approach.
Now you can see the result –
The list has been reversed successfully. So let’s move to another method.
Using A List Slicing Trick
Your second option for reversing list would be List Slicing syntax.
- Python’s list object has an exciting feature called list slicing.
- In this method, basically what you do, you take a list object and you put square parenthesis [] behind it and then you use :: and -1 and it able to create a shallow copy of original list. So it contain all the elements but in reverse order.
- This approach takes more memory because it needs to copy the original list.
- Now list slicing option can be try in need, it is short and concise but the downside of this approach is that this is a more advanced python feature.
- So for some people it could be complex to understand because it is hardest to understand what’s going on.
- But sometime it might be handy and nice.
- So let’s took a look how it can be implemented.
1 2 3 4 5 6 7 8 9 |
mylist = ["Rani", "Jam", "Linda", "Dazy"] print("Original List : ", mylist) my_rev_list = mylist[:: -1] print("Reversed List : ", my_rev_list) |
- my_rev_list = mylist[:: -1] will create a copy of the existing list
- And the copy will contain all the elements but in reverse order.
And the result of this code is –
So now you can see the list has been reversed.
Using reversed() Python Built-in Function
The third option you can use to reverse a list in python is using reversed() method.
- It’s really an exciting option.
- It neither reverses a list in-place, nor does it create a full copy.
- It returns the iterator so we can use it further to cycle through the elements of the list in reverse order.
- It doesn’t modify the original list and not really returning a new list object.
- But instead we just get view into an existing list that we can use to look at all the elements in reverse order.
- So it is really an awesome technique for reversing list in python.
- So let’s see how to implement this.
1 2 3 4 5 6 7 8 9 10 11 |
mylist = ["Rani", "Jam", "Linda", "Dazy"] print("Original List : ", mylist) print("Reversed List : ") for item in reversed(mylist): print(item) |
- reversed(mylist) doesn’t return a real list , it returns a list_reverseiterator object.
- So the interesting thing is that an iterator doesn’t really copy list object and doesn’t really modify the list either.
- But the list constructor built-in keeps iterating until the (reverse) iterator is exhausted, and puts all the elements fetched from the iterator into a new list object.
The output of this code is as follows –
- This is really cool to use reversed() function because it is nice and fast, it doesn’t modify the list and doesn’t copy anything, it just goes trough the elements in reverse order.
Reverse A List Without Using Built-in Functions
Now we will discuss reverse a list without using any built-in functions. So what we have to do, let’s see.
- Technically, to reverse a list you can either transfer the elements in the current list backwards to a new list or you can swap around the elements in the same list object.
- Here i am doing the first one.
- To achieve this, we have to use a while loop in a function, while initializing a new list.
- So the complete code for this is following.
1 2 3 4 5 6 7 8 9 10 11 12 |
def reverse_list(list): new = [] count = len(list)-1 while count>= 0: new.append(list[count]) count -= 1 return new print("Reversed List : ", reverse_list([1,2,3,4])) |
And now you can see the output of the above code.
Conclusion : Which Is Suitable
We have discussed three ways to reverse a list in python. All of them are good and easy to follow. But Now the question is that which is to be used, so my answer is that “it totally depends upon situation”. You can opt anyone from them according to your problem’s needs. You can either use reverse() method, reversed() method, list slicing trick or you can also reverse the list without using any built-in function. All have their upside and downside so my suggestion is that you choose anyone which are best suited for your solution.
Related Articles :
- Join Two Lists Python – Learn Joining Two Lists With Examples
- Python Sort Dictionary By Value – Sort With Lambda Function And Operator Module
- Python Convert String To Datetime – Convert String To Datetime Using datetime Module
- Linear Search Python – Learn Linear Search With Example
So guys, this was all about Reverse A List Python tutorial. I hope it will be helpful for you and you have understood them very well but even if you have any query regarding this then your comments are welcome. And yes please share this with your friends as well as python learners on different social media platform. If you will share it, more and more people can learn from this and that will make me happy. Thanks everyone and be happy.
Reverse a list without using predefined functions.
I have solved your query and updated in the post. Please check it again.