How can I reverse list in Python?

+1 vote
mylist = [1, 2, 3, 4]

This is the list I have, how can I reverse this?

May 13 in Python by Shabnam
• 190 points
15 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+1 vote

Reversing a list is a commonly used operation in Python programming. Lets look into ways of reversing the order of a list in Python:

1. Using [: : -1] list slicing trick

Python list objects have a feature called slicing and slicing a list with [::-1] creates a reversed copy.

>>mylist= [ 1, 2, 3, 4, 5 ]
>>mylist
[1, 2, 3, 4, 5]
>>mylist[::-1]
[5, 4, 3, 2, 1]

By using this trick, the reversed list takes up more memory.  It creates a shallow copy of the list where the container is duplicated but not the individual list elements. The references of the original elements are reused in new copy of container, instead of duplicating the list elements themselves.

2. Using list.reverse() method

Python has a built in reverse() method which one can use to reverse the contents of the list object in-place.

>>mylist= [1, 2, 3, 4, 5 ]
>>mylist.reverse()
>>mylist
[5, 4, 3, 2, 1]

Reversing a list this way takes up less memory as compare to slicing trick, as it directly modifies the original list object instead of creating a new list and copying the existing elements to it in reverse order(as done in slicing trick).  

3. Using reversed() built in function

Lastly, reversing a list using reverse iteration with built in reversed() function. Unlike the above two method, it neither creates a reverse list in-place nor a full copy of list, instead it gets a reverse iterator which can be used to cycle through the elements of list in reverse order.

>>mylist = [1, 2, 3, 4, 5]
>>for revlist in reversed(mylist):
    print(revlist)
5
4
3
2
1

We iterated the elements of a list in reverse order. The original list doesn’t get modified on using reversed() function but it gives a view of all the elements of list  in reverse order .

We call list() constructor on the result of reversed() function, in order to get a reversed copy of the list.

>>mylist
[1, 2, 3, 4, 5]
>>list(reversed(mylist))
[5, 4, 3, 2, 1]

The built-in function uses list constructor to keep on iterating until the reverse iterator is exhausted and all the elements fetched from iterator is placed in a new list object. We get a shallow copy of the original list.

answered May 13 by Taj
• 340 points

Related Questions In Python

0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
425 views
0 votes
1 answer

How can I remove duplicate dict in list in Python?

You can preserve the Order, then you ...READ MORE

answered Nov 19, 2018 in Python by Nymeria
• 3,500 points
414 views
0 votes
1 answer

How can I get the count of a list in Python?

len() >>> mylist=[] >>> print len(mylist) 0 READ MORE

answered Dec 12, 2018 in Python by charlie_brown
• 7,710 points
38 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
25 views
+4 votes
6 answers
0 votes
1 answer

In Python, what is difference between Array and List?

Lists and arrays are used in Python ...READ MORE

answered Mar 15 in Python by Taj
• 340 points

edited Mar 18 by Omkar 129 views
0 votes
1 answer

.join() method is used for what purpose in Python?

join() method is used to concatenate or ...READ MORE

answered May 14 in Python by Taj
• 340 points
10 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.