Your code is actually working as intended; it reverses the list just fine. The reason it prints None
is because list.reverse
is an in-place method that always returns None
.
Because of this, you should be calling reverse
on its own line:
def func4(x):
x.reverse()
print(x)
e = ['baby','egg','chicken']
func4(e)
Now, print
will print the reversed list instead of the None
returned by reverse
.
Note that the same principle applies to list.append
, list.extend
, and pretty much all other methods which mutate objects in Python.
Also, I removed the return-statement from the end of your function since it didn't do anything useful. Python functions already return None
by default, so there is no reason to explicitly do this.