There are multiple ways to reverse a list in Python. One way is to use the built-in 'reverse()' method. Here's an example:

my_list = ['May', 'Mary', 'John']
my_list.reverse()
print(my_list)

Output:

['John', 'Mary', 'May']

Another way is to use slicing with a step of -1. Here's an example:

my_list = ['May', 'Mary', 'John']
reversed_list = my_list[::-1]
print(reversed_list)

Output:

['John', 'Mary', 'May']
How to Reverse a List in Python: Two Simple Methods

原文地址: https://www.cveoy.top/t/topic/lfhL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录