Just wanted to know the difference between reverse() and [::-1] in terms of references.
For example
p = [1,2,3]
x = p[::-1]
print(x)
print(p)
p.reverse()
print(p ==p[::-1])
print(p == x)
so outputs are
[3,2,1]
[1,2,3]
False
True
reverse
reverses the list in-place, see the manual, while [::-1]
gives a new list in reversed order.
Try print(p)
after calling p.reverse()
, you'll see the difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With