[Solved] TypeError: list.append() takes exactly one argument (2 given)

This error means that you’re trying to call list.append() with two arguments. You might be trying to add two items to the list at once, in which case you should use list.extend() or simply call list.append() twice.

Problem: you are passing two arguments to append

If you have a list that you want to add two items to, it might be tempting to try something like the following:

foo = ["a", "b", "c"]
foo.append("d", "e")
Python

Unfortunately, that’s not how the .append() method for lists in Python works. If you try to do this, you’ll end up with the following error message:

Traceback (most recent call last):
  File "/home/user/main.py", line 2, in <module>
    foo.append("d", "e")
TypeError: list.append() takes exactly one argument (2 given)

Just like it says, you can’t pass more than one argument to .append().

Solution 1: use list.extend() instead

If you have two or more items ready to go that you want to add to the list like you would with .append(), you can use .extend() instead like this:

foo = ["a", "b", "c"]

foo.extend(["d", "e"])

print(foo)
# -> ['a', 'b', 'c', 'd', 'e']
Python

The catch is that the items must be an iterable or you’ll get an error. That’s because .extend() only takes one argument as well! Yes, it can be multiple items, but they must be wrapped in a list or a tuple.

You can also use += to do the same thing

Python also lets you just use the + operator to concatenate two lists, which functions the same way as using .extend(). We can combine the + operator with the = operator like this to do it:

foo = ["a", "b", "c"]
foo += ["d", "e"]

print(foo)
# -> ['a', 'b', 'c', 'd', 'e']
Python

Solution 2: call list.append() multiple times

If you have a lot of items to append but you want to do something with them first, it might make more sense for you to just append multiple times in a for loop like this:

foo = ["a", "b", "c"]
items_to_append = ["d", "e"]

for item in items_to_append:
    foo.append(item + "x")

print(foo)
# -> ['a', 'b', 'c', 'dx', 'ex']
Python

Conclusion

If you’re getting the Python error “TypeError: list.append() takes exactly one argument (2 given)”, it means that you passed two arguments to list.append() instead of the required one. You’re probably trying to add more than one item to the list. You’ll need to use list.extend() or the shorthand version of using += to concatenate two lists together, or you’ll just need to call list.append() multiple times.