Skip to main content

How to Resolve Python Error "TypeError: can only concatenate list to list (not "str", not "NoneType")

The TypeError: can only concatenate list (not "X") to list (where "X" is typically "str" or "NoneType") is a common error in Python when working with lists. It signals that you tried to use the addition operator (+) to combine a list with an object of an incompatible type (like a string or None). Python's list concatenation requires both operands to be lists.

This guide explains why this error occurs and provides the correct methods for adding elements or merging lists.

Understanding the Error: List Concatenation Requirements

In Python, the addition operator (+) when used with lists performs concatenation. It creates a new list containing all the elements from the first list followed by all the elements from the second list. Crucially, this operation requires both operands to be lists.

list1 = [1, 2, 3]
list2 = [4, 5]

# ✅ Correct: Concatenating two lists
combined_list = list1 + list2
print(combined_list) # Output: [1, 2, 3, 4, 5]

The TypeError occurs when you attempt to use + with a list and an object that is not a list (e.g., a string, an integer, None).

Cause 1: Concatenating List and String (+ str)

This is the most frequent scenario. You have a list and a string, and you try to "add" the string to the list using +.

Error Scenario

my_tasks = ["Clean room", "Read book"]
new_task = "Water plants" # This is a string

try:
# ⛔️ TypeError: can only concatenate list (not "str") to list
all_tasks = my_tasks + new_task
print(all_tasks)
except TypeError as e:
print(e)

Python doesn't know how to directly "add" a string into a list using the + operator.

Solution: Use list.append() to Add a Single Item

If you want to add the entire string as a single new element to the end of the list, use the list.append() method. This modifies the list in-place.

my_tasks = ["Clean room", "Read book"]
new_task = "Water plants"

# ✅ Use append() to add the string as one item
my_tasks.append(new_task) # Modifies my_tasks directly

print(f"Updated tasks: {my_tasks}")
# Output: Updated tasks: ['Clean room', 'Read book', 'Water plants']

Output:

Updated tasks: ['Clean room', 'Read book', 'Water plants']

Solution: Use list.extend() to Add Items from an Iterable

If you want to add each character of the string as a separate element (less common for this error, but related), or if you want to add all elements from another list, use list.extend(). This also modifies the list in-place.

my_letters = ['a', 'b']
more_letters_str = "cde"
more_letters_list = ['f', 'g']

# Extend with characters from the string
my_letters.extend(more_letters_str)
print(f"Extended with string chars: {my_letters}")
# Output: Extended with string chars: ['a', 'b', 'c', 'd', 'e']

# Extend with items from another list
my_letters.extend(more_letters_list)
print(f"Extended with list items: {my_letters}")
# Output: Extended with list items: ['a', 'b', 'c', 'd', 'e', 'f', 'g']

Output:

Extended with string chars: ['a', 'b', 'c', 'd', 'e']
Extended with list items: ['a', 'b', 'c', 'd', 'e', 'f', 'g']

Solution: Wrap the String in a List for Concatenation

If you specifically want to use the + operator to create a new list with the string added as a single element, you must first wrap the string in its own list.

my_tasks = ["Clean room", "Read book"]
new_task = "Water plants"

# ✅ Wrap the string in a list before concatenating
new_tasks_list = my_tasks + [new_task] # Creates a new list

print(f"Original tasks: {my_tasks}") # Remains unchanged
print(f"New combined list: {new_tasks_list}")
# Output: New combined list: ['Clean room', 'Read book', 'Water plants']

While this works, append() is usually more direct for adding a single item.

Misconception: Trying to Concatenate List Element with String

Sometimes the intent is to combine a string element from the list with another string. Make sure you access the list element first using its index.

my_tasks = ["Clean room", "Read book"]
suffix = " - Priority"

# Incorrect: Trying to add suffix to the whole list
# result = my_tasks + suffix # Raises TypeError

# ✅ Correct: Access the element first, then concatenate strings
first_task_with_suffix = my_tasks[0] + suffix
print(first_task_with_suffix)
# Output: Clean room - Priority

Output:

Clean room - Priority

Cause 2: Concatenating List and None (+ NoneType)

This occurs when one of the variables you're trying to combine with + holds the value None instead of a list.

Error Scenario

list_a = [1, 2]
list_b = None # Variable holds None

try:
# ⛔️ TypeError: can only concatenate list (not "NoneType") to list
combined = list_a + list_b
print(combined)
except TypeError as e:
print(e)

Common Sources of None Values

Variables become None unexpectedly often due to:

  • Functions without an explicit return (they implicitly return None).
  • Functions with conditional return paths that don't cover all cases.
  • Assigning the result of in-place methods like list.sort() or list.append() (which return None) to a variable.
  • Explicit assignment: my_variable = None.

Solution: Ensure Both Operands are Lists (Check for None)

Before attempting concatenation, check if the variable potentially holding None actually holds a list.

list_a = [1, 2]
list_b = None # Might come from a function call, etc.

combined = list_a # Start with list_a

# ✅ Check if list_b is actually a list before concatenating
if isinstance(list_b, list): # Or 'if list_b is not None:' might suffice
combined = combined + list_b # Or combined.extend(list_b)
print("Concatenated list_b.")
elif list_b is None:
print("list_b was None, nothing to add.") # This is executed
else:
print(f"list_b was not None or list, but type {type(list_b)}")

print(f"Final combined list: {combined}") # Output: [1, 2]

Output:

list_b was None, nothing to add.
Final combined list: [1, 2]

Solution: Provide a Default Empty List

Use the or operator to substitute an empty list ([]) if the variable is None. Concatenating with an empty list has no effect but avoids the TypeError.

list_a = [1, 2]
list_b = None # Might be None

# ✅ Use 'or []' to provide an empty list if list_b is None
combined = list_a + (list_b or [])

print(f"Combined list (with 'or []'): {combined}")
# Output: Combined list (with 'or []'): [1, 2]

# Example where list_b IS a list
list_c = [3, 4]
combined_c = list_a + (list_c or [])
print(f"Combined list (list_c): {combined_c}")
# Output: Combined list (list_c): [1, 2, 3, 4]

Output:

Combined list (with 'or []'): [1, 2]
Combined list (list_c): [1, 2, 3, 4]

This is a concise way to handle potential None values during concatenation.

Debugging the Error (type(), isinstance())

When you get this TypeError:

  1. Identify the line causing the error.
  2. Examine the variables involved in the + operation.
  3. Print their types: print(type(variable1)), print(type(variable2)).
  4. One of them will be <class 'list'>, the other will be the type mentioned in the error (<class 'str'> or <class 'NoneType'>).
  5. Use isinstance(variable, list) to explicitly check if a variable holds a list.

Conclusion

The TypeError: can only concatenate list (not "...") to list arises from attempting to use the + operator between a list and a non-list type (commonly str or NoneType).

To fix it:

  • If adding a single item (like a string) to a list: Use my_list.append(item).
  • If adding multiple items from another iterable (like another list or the characters of a string) to a list: Use my_list.extend(iterable).
  • If concatenating lists where one might be None: Check for None first (if other_list is not None:) or use the or [] fallback (list1 + (list2 or [])).
  • Ensure you aren't mistakenly trying to add a string to a list element without first accessing the element by its index.

Understanding the difference between list concatenation (+, requires two lists) and list modification methods (append, extend) is key to resolving this error correctly.