Home >Backend Development >Python Tutorial >How to reverse a list or array using Python

How to reverse a list or array using Python

WBOY
WBOYforward
2023-04-11 22:01:201969browse

How to reverse a list or array using Python

Reversing a list or array is a common programming task. In many cases, you may need to present data in reverse order, such as when sorting a list.

How to reverse a list or array using Python? You'll learn about the different methods in this article.

Create a copy using a for loop

Although Python's for loop is more verbose, it can be convenient in some situations. For example, it provides greater flexibility when performing complex logic at certain points in reverse operations.

Related: How to use For loops in Python https://www.linuxmi.com/python-for-loops.html

Common methods when using indented for loops is to traverse the original list in reverse order. Starting from the last element, each iteration appends the previous element to a new list.

Taking a list of integers between 1 and 9 as an example, here is how to reverse the array using an indented for loop:

languages = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# 创建一个空列表来保存反向数组:
reversed_list = []
# 从原始数组的长度中减去一,以从最后一个索引开始:
reducer = len(languages)-1
# 在for循环中反转列表:
for i in languages:
 reversed_list.append(languages[reducer]) # 将结果追加到空列表
 reducer -=1 # 使用reducer在每次迭代时将索引减少1
print(reversed_list)

Output:

[9, 8, 7, 6, 5, 4, 3, 2, 1]

How to reverse a list or array using Python

Use list comprehensions to reverse a list or array

List comprehensions produce shorter code. And no temporary variables are needed since the list comprehension works on the appropriate list.

To perform the previous operation, use a list comprehension:

reducer = len(languages)
# 在列表推导式中,使用for循环在范围函数中递减索引
Reversed_list = [languages[reducer] for reducer in range(reducer -1,-1,-1)]
print(Reversed_list)

Output:

[9, 8, 7, 6, 5, 4, 3, 2, 1]

How to reverse a list or array using Python

Use the slicing operator

The list slicing operator is very simple, although it has some limitations. For example, you may not be able to customize the output like you can when using a for loop.

Here's how to reverse a list using the slicing operator:

languages = [1, 2, 3, 4, 5, 6, 7, 8, 9]
rev_list = languages[::-1]
print(rev_list)

Output:

[9, 8, 7, 6, 5, 4, 3, 2, 1]

[::-1] The syntax is a neat shortcut that will Produce a reverse list. It actually means "copy each element of the list, counting backwards from the end" - i.e. "reverse it"!

How to reverse a list or array using Python

Using the inversion method of an array

This is another efficient method: it modifies the original array. This can be a disadvantage since you can't keep the previous list for other operations.

The following is how to reverse an array using the reverse method:

languages = [1, 2, 3, 4, 5, 6, 7, 8, 9]
languages.reverse()
print(languages)

Output:

[9, 8, 7, 6, 5, 4, 3, 2, 1]

How to reverse a list or array using Python

Use the reversed function

reversed function iterates over a list, array, or any other sequence and returns its reversed copy. However, you need to explicitly declare the reverse output as a list.

Here's how it works:

languages = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list(reversed(languages)))

Output:

[9, 8, 7, 6, 5, 4, 3, 2, 1]

How to reverse a list or array using Python

Get creative with arrays

Array or Lists are a common way to store data. Depending on your goals, you may want to present the data to the client in reverse order. One way is to reverse the array or list before rendering. As you can see, there are several ways to reverse a list in Python. Choose the approach that works best for you and is consistent with your logic for your specific problem.

The above is the detailed content of How to reverse a list or array using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete