Why does list.reverse() return None in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
2 min

banner

# Why does list.reverse() return None in Python

The list.reverse() method returns None because it mutates the original list. Most methods that mutate an object in place return None in Python.

main.py
a_list = ['bobby', 'hadz', '.', 'com'] result = a_list.reverse() print(result) # ๐Ÿ‘‰๏ธ None print(a_list) # ๐Ÿ‘‰๏ธ ['com', '.', 'hadz', 'bobby']

list reverse method returns none

The code for this article is available on GitHub

The list.reverse() method reverses the elements of the list in place.

The method returns None because it mutates the original list.

It is a convention in Python for methods that mutate the original object to return None.

# Reversing the list without mutating it

If you want to reverse the list without mutating it in place, use the reversed() function.

main.py
a_list = ['bobby', 'hadz', '.', 'com'] result = list(reversed(a_list)) print(result) # ๐Ÿ‘‰๏ธ ['com', '.', 'hadz', 'bobby'] print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']

reversing list without mutating it

The code for this article is available on GitHub

The reversed() function takes an iterator, reverses it and returns the result.

The function returns the reversed list as an iterator, so we had to use the list() class to convert the iterator to a list.

The reversed() function doesn't mutate the original list. The original list remains unchanged.

You can also reassign the variable if you don't need to keep the original list around.

main.py
a_list = ['bobby', 'hadz', '.', 'com'] a_list = list(reversed(a_list)) print(a_list) # ๐Ÿ‘‰๏ธ ['com', '.', 'hadz', 'bobby']

# Using list slicing to reverse the list

Alternatively, you can use the list slicing syntax with a step of -1 to reverse the list and return the result.

main.py
a_list = ['bobby', 'hadz', '.', 'com'] result = a_list[::-1] print(result) # ๐Ÿ‘‰๏ธ ['com', '.', 'hadz', 'bobby'] print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']

using list slicing to reverse the list

The code for this article is available on GitHub

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

If the start index is omitted, it is considered to be 0, if the stop index is omitted, the slice goes to the end of the list.

We used a step of -1 to reverse the list. When a negative step is used, we select every Nth element from the end of the list toward the beginning.

Negative indices can be used to count backward, e.g. my_list[-1] returns the last item in the list and my_list[-2] returns the second to last item.

You can reassign the a_list variable if you don't need to keep the original list around.

main.py
a_list = ['bobby', 'hadz', '.', 'com'] a_list = a_list[::-1] print(a_list) # ๐Ÿ‘‰๏ธ ['com', '.', 'hadz', 'bobby'] print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']
The code for this article is available on GitHub

Don't try to assign the result of calling reverse(), append(), insert() or extend() to a variable because methods that mutate the original object most commonly return None.

# Using your IDE to check a method's return type

Most of the time, you can hover over the method in your IDE to see the method's return value.

If a method returns None, then it mutates an object in place.

Conversely, most methods that return a value other than None, usually return a new object and don't mutate the original one.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev