How to resolve TypeError: can only concatenate list (not NoneType) to list in Python

If you are looking for a solution to the error TypeError: can only concatenate list (not NoneType) to list. To fix this, the variable or function value must be a value other than None and check the variable data type before passing it to the function. Details are below.

What causes the TypeError: can only concatenate list (not NoneType) to list error?

The TypeError: can only concatenate list (not NoneType) to list error happens because you’re concatenating a list with a None value.

Example

listInfor = ['learnshareit', 'website']
invalidVar = None

# Concatenating a list with a None value will cause an error.
print(listInfor + invalidVar )

Output:

Traceback (most recent call last):
 File "./prog.py", line 5, in <module>
TypeError: can only concatenate list (not "NoneType") to list

The error TypeError: can only concatenate list (not “NoneType”) to list also occurs when you declare a function whose return value is not explicitly.

Example:

listInfor = ['learnshareit', 'website']

def noValueFunc():
    print (['visit'])   

noValue = noValueFunc()
print(listInfor + noValue)

Output:

Traceback (most recent call last):
 File "./prog.py", line 7, in <module>
TypeError: can only concatenate list (not "NoneType") to list

How to solve this error?

Check the NoneType object.

You can use the Authentication operator to check if a variable can validly perform a concatenation.

Example:

  • Declare a variable whose value is None.
  • Use the Authentication operator. If the variable contains the value None, execute the if statement; otherwise, you can join two objects together.
  • You can replace the ‘is’ operator with the ‘is not’ operator (substitute statements accordingly).
invalidVar = None
listInfor = ['learnshareit', 'website']

# Use the 'is' operator
if invalidVar is None:
    print('Object is None')
else:
    print('Object is not None')
    print(invalidVar + listInfor)     

Output:

Object is None

You can use the relational operator ‘!=’ for error handling.

The ‘!=’ operator compares the values ​​of the arguments: if they are different, it returns True. If equal, returns False.

Example:

invalidVar = None
listInfor = ['learnshareit', 'website']

# Use the relational operator '!='
if invalidVar != None:
    print('Object is not None')
    print(invalidVar + listInfor)
    
else:
     print('Object is None')

Output:

Object is None

Use the isinstance() function to check NoneType. 

Example:

invalidVar = None
listInfor = ['learnshareit', 'website']

if isinstance(invalidVar, type(None)) is True:
   print('Object is None')

else:
    result = invalidVar + listInfor
    print(result)

Output:

Object is None

Avoid initializing functions that do not return results or return None.

Create can initialize a variable that is a list or a string as long as it is not None, then concatenating two objects will not cause an error.

Example:

invalidVar = ['visit']
listInfor = ['learnshareit', 'website']

# Use the 'is' operator
if invalidVar is None:
    print('Object is None')

else:
    print('Object is not None')
    result = invalidVar + listInfor     
    print(result)

Output:

Object is not None
['visit', 'learnshareit', 'website']

When you instantiate a function that needs to return an explicit value, which can be a list containing elements, the concatenation will not cause an error.

Example:

listInfor = ['learnshareit', 'website']

def ValueFunc():
    return (['visit'])   

Value = ValueFunc()
print(Value + listInfor)

Output:

['visit', 'learnshareit', 'website']

Summary

The problem TypeError: can only concatenate list (not NoneType) to list in Python is probably solved. If you have any questions or have a more creative way, I hope you will share it with everyone by commenting below. Thank you for reading my article.

Leave a Reply

Your email address will not be published. Required fields are marked *