Borislav Hadzhiev
Last updated: Apr 20, 2022
Check out my new book
The Python "TypeError: list.append() takes exactly one argument (2 given)"
occurs when we pass multiple arguments to the list.append
method. To solve the
error, either pass a list containing the arguments to the append
method, or
use the extend()
method.
Here is an example of how the error occurs.
my_list = ['a', 'b'] # ⛔️ TypeError: list.append() takes exactly one argument (2 given) my_list.append('c', 'd')
If you have a list of lists (a two-dimensional list) or a list of tuples, group
the arguments in a collection and pass a single value to the append()
method.
my_list = [['a', 'b']] my_list.append(['c', 'd']) print(my_list) # 👉️ [['a', 'b'], ['c', 'd']]
The list.append() method adds an item to the end of the list.
The append()
method takes a single value as an argument.
The method returns None
as it mutates the original list.
If you meant to append multiple items to the list, use the list.extend()
method instead.
my_list = ['a', 'b'] my_list.extend(['c', 'd', 'e']) print(my_list) # 👉️ ['a', 'b', 'c', 'd', 'e']
The list.extend method takes an iterable (such as a list) and extends the list by appending all of the items from the iterable.
The list.extend
method returns None
as it mutates the original list.
Notice that both list.append()
and list.extend()
take a single argument - an
iterable.
So, if we pass multiple, comma-separated arguments to either of the methods, the error is caused.