This error means you gave two arguments to list.extend()
, which only takes one. You probably are trying to add two items to the list. The fix is simple, just wrap those two items in brackets ([]) to make them a list, and therefore only one argument.
Table of Contents
Problem: you passed 2 arguments to list.extend()
If you have two items you want to add to a list, and you try to pass them by themselves as arguments to extend like the following, it won’t work:
foo = ["a", "b", "c"]
foo.extend("d", "e")
PythonInstead, you’ll get the following stack trace:
Traceback (most recent call last):
File "/home/user/main.py", line 2, in <module>
foo.extend("d", "e")
TypeError: list.extend() takes exactly one argument (2 given)
Just as it says, you can’t add two items to the list by passing them as separate arguments. Instead, you’ll have to make them only one argument.
Solution 1: wrap the two arguments in a list
This is one way to make your two arguments into one argument:
foo = ["a", "b", "c"]
foo.extend(["d", "e"])
print(foo)
# -> ['a', 'b', 'c', 'd', 'e']
PythonSolution 2: wrap the two arguments in a tuple
Alternatively, you can use parenthesis ()
to make your two items into a single argument:
foo = ["a", "b", "c"]
foo.extend(("d", "e"))
print(foo)
# -> ['a', 'b', 'c', 'd', 'e']
PythonConclusion
In Python, you can’t pass more than one argument to list.extend()
. It expects another list
, or a tuple
, or some other kind of iterable as its only argument. So, to fix it, just wrap your items in brackets or parenthesis to make them into a single list
or tuple
respectively before passing them to list.extend()
.
John is a professional software engineer who has been solving problems with code for 15+ years. He has experience with full stack web development, container orchestration, mobile development, DevOps, Windows and Linux kernel development, cybersecurity, and reverse engineering. In his spare time, he’s researching the potential business applications of AI.