Python list question

I have a question about how lists work. I have a theory and I’d like confirmation of whether I’m correct or not.

The following code reverses a list.

list1 = [100, 200, 300, 400, 500]
list1.reverse()
print(list1)

However, when I assign the list to an additional variable…

list1 = [100, 200, 300, 400, 500]
resolution = list1.reverse()
print(resolution)

It returns None. Type() indicates it is a none type with no defined content.

I am assuming that this is because the container of resolution is empty and does not recognize that instance of the reverse method of list1. The result is an undefined variable.

Am I on the right track?

list1.reverse() doesn’t return reversed list, the list is reversed in place.

Functions and methods always return something, however if they don’t do that explicitly (with return keyword), then None is returned.

def explicit_return():
    a = 1 + 2
    return a

def implicit_return():
    a = 1 + 2

print(explicit_return())   # 3
print(implicit_return())   # None
3 Likes

You can go deep on this here: https://realpython.com/python-reverse-list/

The above behaviour is one of the first things discussed.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.