Reputation: 11
As the title says, I have the following array,
arr = [7, 69, 2, 221, 8974]
I then reverse it using either of the following methods
In [01]: arr[::-1]
Out[01]: [8974, 221, 2, 69, 7]
Using .reverse()
In [01]: arr.reverse()
In [02]: print(arr)
Out[02]: [8974, 221, 2, 69, 7]
Using reversed(arr)
In [01]: list(reversed(arr))
Out[01]: [8974, 221, 2, 69, 7]
Clearly in all instances the output should be [8974, 221, 69, 7, 2]
I am using Python 3.9.5. Does anyone know why this behaviour is observed?
Upvotes: 0
Views: 46
Reputation: 2084
I think you want your output to be sorted. You can try code in this way:
arr = [7, 69, 2, 221, 8974]
sorted(arr,reversed=True)
Using .reverse()
In [01]: arr.sort().reverse()
In [02]: print(arr)
Out[02]: [8974, 221, 2, 69, 7]
Using reversed(arr)
In [01]: list(reversed(sorted(arr)))
Out[01]: [8974, 221, 2, 69, 7]
Upvotes: 5