Last updated: Apr 8, 2024
Reading timeยท3 min
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')
The list.append()
method takes a single value and adds it to the end of the
list.
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.
list.extend()
to append multiple items to the listIf 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.
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.
Here is an example of how the error occurs.
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.
One way to solve the error is to use the addition (+) operator to concatenate the two strings.
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')
We replaced each comma with a plus +
to concatenate the two strings.
print('a' + 'b' + 'c') # ๐๏ธ 'abc'
An alternative approach is to use a formatted string literal.
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')
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.
open()
function instead of the with
statementAlternatively, you can store the file object into a variable and manually close it.
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.
You can learn more about the related topics by checking out the following tutorials: