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
• 920 points
56 views

1 answer to this question.

+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
• 1,040 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
860 views
+1 vote
2 answers

How can I remove duplicate dict in list in Python?

[dict(t) for t in {tuple(d.items()) for d ...READ MORE

answered Aug 15 in Python by anonymous

edited Aug 20 by Kalgi 1,851 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,720 points
105 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 413 views
+4 votes
6 answers
0 votes
1 answer

How do I loop backward using indices in Python?

Hi, You can make use of negative indices ...READ MORE

answered Jul 3 in Python by Taj
• 1,040 points
31 views
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
• 1,040 points

edited Mar 18 by Omkar 9,429 views