Skip to main content

Command Palette

Search for a command to run...

Python Roads of Reverse List

Published
•3 min read•View as Markdown

In Python programming, we often need to reverse a list. Suppose there is a list: [1, 2, 3, 4, 5], we need to get the inverse sequence list of this list, [5, 4, 3, 2, 1].

First, let us prepare the source list:

>>> source_list = [1, 2, 3, 4, 5]

Road One: For Loop

The honest method of reverse a list is to use a for loop. Starting from the end of the source list, and taking the item in reverse order.Add the item to the end of target list.

>>> target_list = list()
>>> for index in range(len(source_list)):
>>> target_index =-index -1
>>> target_list.append(source_list[target_index])
>>> print(target_list)
[5, 4, 3, 2, 1]

Of course, if you use the list comprehension, the above content can be written as one line of code:

>>> print([source_list[(- index -1)] for index in range(len(source_list))])
[5, 4, 3, 2, 1]

Road Two: Reverse method of list

Of course, the above method is a bit complicated, and we need a more concise method. According to the official documentation, the list has a reverse method, which function is to reverse the order of the list. For example:

>>> source_list.reverse()
>>> print(source_list)
[5, 4, 3, 2, 1]

This method is simple and straightforward. But be aware that this method will change the list itself. If we don't want to change its content, we need to create a copy of the list first. Examples are as follows:

>>> source_list = [1, 2, 3, 4, 5]
>>> target_list = source_list[:]
>>> target_list.reverse()
>>> print(source_list)
>>> print(target_list)
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]

Road Three: Reversed class

Is it troublesome to create a copy? Then use the Python built-in reserved class. The official documentation is: Return a reverse iterator. seq must be an object which has a __reversed__() method or supports the sequence protocol (the __len__() method and the __getitem__() method with integer arguments starting at 0). For example:

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

Note it returns an iterator, so we use the list class to convert the iterator to a list. If you just need to know how to reverse a list, then use reverse and reversed is enough. They are all Python built-in object, efficient and convenient, and you don't suffer a loss when you use them.

The following content is only reserved for friends who have spare time to learn and have nothing to do.

Road Four: List Slice

Probably no one who uses Python doesn't know the list. Those who use the list, probably no one doesn’t know the list Sliced. Using slices, some people may notice that you can slice like this:

>>> print(source_list[::-1])
[5, 4, 3, 2, 1]

Road Five: Slice object

Python also has a slice object, which can be used to reverse the sequence list in a similar way:

>>> print(source_list[slice(None, None, -1)])
[5, 4, 3, 2, 1]

Road six: Artificial mental retardation

Now the most popular Python is probably artificial intelligence, unfortunately it is not available here for the time being. Avenue to Jane, and finally We give the most direct artificial mental retardation solution:

>>> print([5, 4, 3, 2, 1])
[5, 4, 3, 2, 1]
S

the python utility 'reverse' is reversing in-place. if you need a new list of reversed items? In such case the original list should be copied (by the member utility 'copy') into the new list and the function 'revers' applied on the new list: a=[1,2,3,4,5]

b=a.copy()

'b' and 'a' are different objects

id(a) 80088808

id(b) 8526344

print(b) [ 1,2,3,4,5]

b.reverse()

print(b) [5, 4, 3, 2, 1]