ls = list(range(10))
ls.reverse()
print(ls)
Why does this work in producing a list that counts backwards from 9 not but not this:
ls = list(range(10)).reverse()
print(ls)
These last two lines prints this instead:
None
Shouldn't they be the same thing?
No, because list.reverse() returns None since it reverses the list in place. See the list documentation
You could use reversed() like so:
countdown = list(reversed(range(10)))
print(countdown)
See the reversed documenation
See also this question
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