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, 2019 in Python by Shabnam
• 930 points
985 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, 2019 by Taj
• 1,080 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
3,458 views
+1 vote
3 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, 2019 in Python by anonymous

edited Aug 20, 2019 by Kalgi 9,290 views
0 votes
2 answers

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

n=[1,2,3,4,5,6,7,8,9] print(len(n)) =9 READ MORE

answered Dec 10, 2020 in Python by anonymous
1,486 views
0 votes
1 answer
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,071 views
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 4, 2019 in Python by Taj
• 1,080 points
655 views
+1 vote
4 answers

In Python, what is difference between Array and List?

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

answered Mar 15, 2019 in Python by Taj
• 1,080 points

edited Mar 18, 2019 by Omkar 152,789 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP