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

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Table of Contents

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

# 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')

The list.append() method takes a single value and adds it to the end of the list.

# Grouping the elements in a collection

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']]

grouping the elements in a collection

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.

# Using list.extend() to append multiple items to the 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']

using list extend to append multiple items to the list

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.

# TextIOWrapper.write() takes exactly one argument (2 given)

The Python "TypeError: TextIOWrapper.write() takes exactly one argument (2 given)" occurs when we pass multiple arguments to the write() method.

To solve the error, use the addition (+) operator to concatenate the strings in the call to the method.

typeerror textiowrapper write takes exactly one argument 2 given

Here is an example of how the error occurs.

main.py
file_name = 'example.txt' with open(file_name, 'w', encoding='utf-8') as my_file: # โ›”๏ธ TypeError: TextIOWrapper.write() takes exactly one argument (2 given) my_file.write('first line', '\n') my_file.write('second line', '\n') my_file.write('third line', '\n')

Notice that we separate the two strings using a comma, so we are passing 2 arguments to the write() method, but the method takes a single string.

# Use the addition (+) operator to solve the error

One way to solve the error is to use the addition (+) operator to concatenate the two strings.

main.py
file_name = 'example.txt' with open(file_name, 'w', encoding='utf-8') as my_file: my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n')

use addition operator to solve the error

We replaced each comma with a plus + to concatenate the two strings.

main.py
print('a' + 'b' + 'c') # ๐Ÿ‘‰๏ธ 'abc'

# Using a formatted string literal to solve the error

An alternative approach is to use a formatted string literal.

main.py
file_name = 'example.txt' first = 'first line' second = 'second line' third = 'third line' with open(file_name, 'w', encoding='utf-8') as my_file: my_file.write(f'{first}\n') my_file.write(f'{second}\n') my_file.write(f'{third}\n')
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

The with open() syntax takes care of automatically closing the file even if an exception is thrown.

# Using the open() function instead of the with statement

Alternatively, you can store the file object into a variable and manually close it.

main.py
file_name = 'example.txt' first = 'first line' second = 'second line' third = 'third line' my_file = open(file_name, 'w', encoding='utf-8') my_file.write(f'{first}\n') my_file.write(f'{second}\n') my_file.write(f'{third}\n') my_file.close()

Note that it's better to use the with open() syntax as it automatically closes the file after we are done.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev