TypeError: can only concatenate list (not "str") to list
1 · The lesson
readWhat 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
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:
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.
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:
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():
names = ["Ada"] names.append("Grace") print(names) # ['Ada', 'Grace']
Or put the item in brackets so both sides are lists:
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:
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:
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 + iteminstead ofresults.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 ofreturn []. - Joining file paths stored in a list with
+ "/data.csv". Useos.path.joinorpathlib.Pathon the parts instead. - Adding a dict to a list of dicts with
+, which givescan only concatenate list (not "dict") to list. Use.append(new_dict).
Related errors
- TypeError: unsupported operand type(s) for +: 'int' and 'str': the same
+mistake with a string on the left. - AttributeError: 'str' object has no attribute 'X': the opposite mix-up, calling
.append()on a string. - AttributeError: 'list' object has no attribute 'X': calling a string method on the list itself.
- TypeError: unsupported operand type(s) for +: 'NoneType' and 'int':
+with aNone, often frommy_list = my_list.append(x).
See Also
- All Python errors: the full index, by type and by when it happens.
- Lists (tutorial)
- Lists cheat sheet
- Loops (tutorial)
Found this useful? Share it with a friend who’s learning Python.