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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

TypeError: list.append takes exactly one argument (2 given) #

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.

typeerror list append takes exactly one argument 2 given

Here is an example of how the error occurs.

main.py
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.

main.py
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.

main.py
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.