Extending list returns None in Python

Extending list returns None in Python is a Python issue which may occur when you work with list.extend and make a mistake.

In this article, I am going to try and fix the issue for you while I try to explain why the issue is popping up. I will present a solution that have worked for me and for other developers who faced the issue ‘Extending list returns None in Python’ .

Explaining the issue : Extending list returns None in Python

The issue usually occurs when you do not know how to work with list.extend in Python.

There is no error message for this particular issue. Before we get into the fix, you might be wondering how we can replicate the issue. The issue can happen as follows.

Let us say that our goal is to add a string to a list called mylist.

First, we start with the line of code bellow.

                                                                       #
mylist = ['car','pc','phone','wallet','keys','shirt','charger']
                                                                       #

Now, ‘bottle’ is the string we want to add.

                                                                       #
addthis = 'bottle'
                                                                       #

Now, we add the string element to our list mylist

                                                                       #
mylist = mylist.extend(addthis)
                                                                       #

This is the final code.

                                                                       #
mylist = ['car','pc','phone','wallet','keys','shirt','charger']
addthis = 'bottle'
mylist = mylist.extend(addthis)
                                                                       #

This is not going to add the string and it is not going to throw an error, all what you are going to get is: none.

Bellow, we will explain the issue while actually solving it at the same time.

Solution : Use extend the correct way

First of all, you need to understand that extend in python needs to be re-assigned back to the list variable while using brackets like this: mylist.extend([addthis]) which we did not do above.

So, the code bellow

                                                                       #
mylist = ['car','pc','phone','wallet','keys','shirt','charger']
addthis = 'bottle'
mylist = mylist.extend(addthis)
                                                                       #

Will be replaced by the following code

                                                                       #
mylist = ['car','pc','phone','wallet','keys','shirt','charger']
addthis = 'bottle'
mylist.extend([addthis])
                                                                       #

Now, instead of returning none, the code will return the following list.

                                                                       #
['car','pc','phone','wallet','keys','shirt','charger','bottle']
                                                                       #

Notice how we stopped doing ‘mylist = mylist.extend(addthis)‘ and removed ‘mylist =’ completely.

The solution above should be enough to solve the problem, if you like our effort make sure to consider donating to our Kofi account, there is a red button that you can use if you are feeling generous.

Summing-up

This is the end of our article, this issue could be confusing but it could be solved easily by following the fixes above, thank you for reading.

To support us consider donating to our Kofi account above. Keep coding, keep learning and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/