3
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?

  • 2
    because `.reverse` works in-place, and so by convention it returns `None`. There is no reason to believe they should be the same thing. They can and do work differently. Note, you can just do `list(reversed(range(10))` – juanpa.arrivillaga Feb 12 '19 at 01:16

1 Answers1

1

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

John Cummings
  • 1,949
  • 3
  • 22
  • 38