0

I'm wondering why the variable lengthOfList can store len(l) but the variable reverseOfList cannot store a method being called on a list. If I call l.reverse() and print(l) I can see the reverse but why can't the reversed list be stored in a variable?

Please see example code below.

    l = [1,2,3]

    lengthOfList = len(l) #3

    reverseOfList = l.reverse() # Global frame None

    print(reverseOfList) #None
Dan
  • 45,079
  • 17
  • 88
  • 157
Eug
  • 165
  • 1
  • 2
  • 11

1 Answers1

1

From Python documentation:

list.reverse()
  Reverse the elements of the list in place.

This means the list is reversed in place, and the function returns None

You should try

>>> l = [1, 2, 3]
>>> l.reverse()
>>> print(l)
[3, 2, 1]
gogaz
  • 2,323
  • 2
  • 23
  • 31