PythonMastery
reference 3 min read · lesson 65 of 80 in Errors

TypeError: can only concatenate list (not "str") to list

1 · The lesson

read

What this error means

You used + with a list on the left and something that is not a list on the right. For lists, + means "join two lists into one longer list", so both sides must be lists. The type in brackets tells you what you gave it instead: "str", "int", "dict" and so on. Most of the time you wanted to add one item to the list, and the method for that is .append().

When you see it

python
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    names = names + "Grace"
            ~~~~~~^~~~~~~~~
TypeError: can only concatenate list (not "str") to list

With a number, the message names int:

python
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    nums = nums + 3
           ~~~~~^~~
TypeError: can only concatenate list (not "int") to list

Why it happens

1. You meant "add this item". + doesn't add a single item to a list. It joins two lists.

python
names = ["Ada"]
names = names + "Grace"       # TypeError: not "str"

2. One side changed type somewhere else. A function returned a single value instead of a list, or a variable that started as a list was later set to a string. The line with + is right, but one of its values is not what you expect.

3. += doesn't give this error, and that hides a different bug. += on a list accepts any sequence. A string is a sequence of letters, so each letter becomes its own item:

python
names = ["Ada"]
names += "Grace"
print(names)                  # ['Ada', 'G', 'r', 'a', 'c', 'e']

No error, but the list is wrong. If you see single letters in a list, look for a += with a string.

How to fix it

Add one item with .append():

python
names = ["Ada"]
names.append("Grace")
print(names)                  # ['Ada', 'Grace']

Or put the item in brackets so both sides are lists:

python
names = ["Ada"]
names = names + ["Grace"]
print(names)                  # ['Ada', 'Grace']

nums = [1, 2]
print(nums + [3])             # [1, 2, 3]

Add several items with .extend(), or + with another list:

python
names = ["Ada"]
names.extend(["Grace", "Linus"])
print(names)                  # ['Ada', 'Grace', 'Linus']

Fix the += case the same way: names += ["Grace"] adds one item, while names += "Grace" adds five letters.

If you wanted text, not a list, join the list into a string first:

python
names = ["Ada", "Grace"]
print(", ".join(names) + " and Linus")   # Ada, Grace and Linus

When you'd see this in real code

  • Building a list in a loop with results = results + item instead of results.append(item).
  • A function that returns a list most of the time but a single string in one branch, for example return "none found" instead of return [].
  • Joining file paths stored in a list with + "/data.csv". Use os.path.join or pathlib.Path on the parts instead.
  • Adding a dict to a list of dicts with +, which gives can only concatenate list (not "dict") to list. Use .append(new_dict).

See Also

Found this useful? Share it with a friend who’s learning Python.