Detailed explanation of Python list reverse() function

"Author's Homepage": Shibie Sanri wyx
"Author's Profile": CSDN top100, Alibaba Cloud Blog Expert, Huawei Cloud Share Expert, Network Security High-quality Creator
"Recommended Column": Xiaobai Zero Basic "Python Beginner to Master"

reverse() can "reverse" the function in the list

grammar

list.reverse()

return value

  • None , no return value, modifies the list, reversing the order of elements.

Example: Reverse the order of elements in a list

list1 = [1, 2, 3]
list1.reverse()
print(list1)

output:

[3, 2, 1]

1. reverse() will modify the original copy

reverse() is designed to reverse the original list and return None.

The advantage of this design is that there is no need to create a new list, thereby reducing the use of "resources" .

We receive the return value of reverse() , print it, and find that it is None.

list1 = [1, 2, 3]
list2 = list1.reverse()
print(list2)

output:

None

Let's take a look at the memory address before and after the reversal

list1 = [1, 2, 3]
print('反转前:', id(list1))
list1.reverse()
print('反转前:', id(list1))

output:

反转前: 2127562869952
反转前: 2127562869952

It can be found that the memory address has not changed, but the content is different, which means that the data before the reversal "disappears" , which needs to be paid attention to when using it.

2. What is the difference between reverse() and reversed()?

reversed() can also reverse the list, while "not modifying" the original list.

list1 = [1, 2, 3]
list2 = list(reversed(list1))
print(list1)
print(list2)

output:

[1, 2, 3]
[3, 2, 1]

reversed() returns the following "reverse iterator" , which needs to be converted into a list to be used normally.

list1 = [1, 2, 3]
list2 = reversed(list1)
print(list2)

output:

<list_reverseiterator object at 0x000002414D7CA880>

3. Other types

Types such as strings, tuples, and dictionaries cannot use the reverse() function, otherwise an error AttributeError: 'str' object has no attribute 'reverse' will be reported.

insert image description here

insert image description here

insert image description here

Of course, these types can be reversed using reversed() , with list comprehensions:

str1 = 'abc'
print([x for x in reversed(str1)])

tuple1 = (1, 2, 3)
print(tuple(reversed(tuple1)))

dict1 = {
    
    'key1': 1, 'key2': 2}
print([x for x in reversed(dict1)])

output:

['c', 'b', 'a']
(3, 2, 1)
['key2', 'key1']

It should be noted that after the list and tuple reversed() are reversed, they can be converted to the corresponding type;
but after the string is reversed, the conversion type is invalid; and after the tuple is reversed, the conversion type will report an error:

str1 = 'abc'
print(str(reversed(str1)))

dict1 = {
    
    'key1': 1, 'key2': 2}
print(dict(reversed(dict1)))

output:
insert image description here


4. Use slices to reverse elements

Through the "slicing" method, the elements and formats in the list are reversed [start:end:step], and this method will not change the original list.

list1 = [1, 3, 2, 5]
print(list1[::-1])
print(list1)

output:

[5, 2, 3, 1]
[1, 3, 2, 5]

Guess you like

Origin blog.csdn.net/wangyuxiang946/article/details/131596619