How to Resolve Python "TypeError: X takes exactly one argument (N given)"
The TypeError: X takes exactly one argument (N given)
(where X is often list.append
or TextIOWrapper.write
, and N is greater than 1) is a common Python error. It indicates that you have called a method that is designed to accept only a single argument, but you mistakenly provided multiple arguments, usually separated by commas.
This guide explains why this error occurs for methods like list.append()
and file.write()
, and provides the correct ways to pass data to them.
Understanding the Error: Method Signatures
Methods in Python have defined "signatures" that specify the number and type of arguments they expect. The TypeError: ... takes exactly one argument (N given)
error means the method you called was defined to accept precisely one argument (excluding self
for instance methods), but your call provided N arguments instead (where N > 1).
Error 1: TypeError: list.append() takes exactly one argument (N given)
Cause: Passing Multiple Items to append()
The list.append()
method is designed to add one single item to the end of a list. If you try to pass multiple values separated by commas, Python interprets this as multiple arguments, violating the method's signature.
my_list = ['a', 'b']
print(f"Original list: {my_list}")
# Error Scenario: Passing 'c' and 'd' as separate arguments
try:
# ⛔️ TypeError: list.append() takes exactly one argument (2 given)
my_list.append('c', 'd')
print(my_list)
except TypeError as e:
print(e)
Solution 1: Pass a Single Item (e.g., a List or Tuple)
If you intend to add a collection (like another list or a tuple) as a single element to the end of your list, enclose those items within their own list []
or tuple ()
and pass that single collection object to append()
.
my_list_of_lists = [['a', 'b']]
print(f"Original list: {my_list_of_lists}")
# Output: Original list: [['a', 'b']]
# ✅ Append a LIST containing ['c', 'd'] as ONE new element
my_list_of_lists.append(['c', 'd'])
print(f"After appending a list: {my_list_of_lists}")
# Output: After appending a list: [['a', 'b'], ['c', 'd']]
my_list_of_tuples = [('x', 'y')]
# ✅ Append a TUPLE ('z', 'w') as ONE new element
my_list_of_tuples.append(('z', 'w'))
print(f"After appending a tuple: {my_list_of_tuples}")
# Output: After appending a tuple: [('x', 'y'), ('z', 'w')]
Output:
Original list: [['a', 'b']]
After appending a list: [['a', 'b'], ['c', 'd']]
After appending a tuple: [('x', 'y'), ('z', 'w')]
Solution 2: Use list.extend()
for Multiple Items
If your goal was to add each individual item from another collection to the end of your list (flattening it), use the list.extend()
method instead. extend()
takes exactly one argument: an iterable (like a list or tuple).
my_list = ['a', 'b']
items_to_add = ['c', 'd', 'e'] # The iterable to add
print(f"Original list: {my_list}")
# Output: Original list: ['a', 'b']
print(f"Items to extend with: {items_to_add}")
# Output: Items to extend with: ['c', 'd', 'e']
# ✅ Use extend() to add each item from the iterable individually
my_list.extend(items_to_add)
print(f"List after extend: {my_list}")
# Output: List after extend: ['a', 'b', 'c', 'd', 'e']
Output:
Original list: ['a', 'b']
Items to extend with: ['c', 'd', 'e']
List after extend: ['a', 'b', 'c', 'd', 'e']
append()
adds one element (which could be a list).extend()
adds multiple elements from an iterable. Both take only one argument.
Error 2: TypeError: TextIOWrapper.write() takes exactly one argument (N given)
Cause: Passing Multiple Strings to write()
The file.write()
method (where file
is an object returned by open()
often called TextIOWrapper
) is designed to write one single string to the file. Passing multiple strings separated by commas results in the TypeError
.
filename = "output.txt"
line1 = "First line."
newline = "\n"
# Error Scenario: Passing two string arguments
try:
with open(filename, 'w', encoding='utf-8') as f:
# ⛔️ TypeError: TextIOWrapper.write() takes exactly one argument (2 given)
f.write(line1, newline) # Incorrect: two arguments
except TypeError as e:
print(e)
except Exception as e:
print(f"Other error: {e}")
Solution 1: Concatenate Strings Before Writing (+
)
Combine the strings you want to write into a single string using the +
operator before passing it to write()
.
filename = "output_plus.txt"
line1 = "First line."
newline = "\n"
try:
with open(filename, 'w', encoding='utf-8') as f:
print(f"\nWriting to {filename} using '+'...")
# ✅ Concatenate into ONE string argument before calling write()
f.write(line1 + newline)
f.write("Second line." + newline)
print("Write successful.")
# Check file content externally
except Exception as e:
print(f"Error: {e}")
Solution 2: Use f-Strings
Formatted string literals (f-strings) provide a clean way to embed variables and literals into a single string argument for write()
.
filename = "output_fstring.txt"
line_num = 1
content = "Data point"
newline = "\n"
try:
with open(filename, 'w', encoding='utf-8') as f:
print(f"\nWriting to {filename} using f-string...")
# ✅ Create ONE string argument using an f-string
f.write(f"Line {line_num}: {content}{newline}")
line_num += 1
content = "Another value"
f.write(f"Line {line_num}: {content}{newline}")
print("Write successful.")
# Check file content externally
except Exception as e:
print(f"Error: {e}")
Solution 3: Make Multiple write()
Calls
If you have separate pieces of data to write sequentially, simply call write()
multiple times, each with a single string argument.
filename = "output_multi.txt"
part1 = "Log entry: "
part2 = "Error occurred."
part3 = "\n"
try:
with open(filename, 'w', encoding='utf-8') as f:
print(f"\nWriting to {filename} using multiple calls...")
# ✅ Call write multiple times, each with ONE argument
f.write(part1)
f.write(part2)
f.write(part3)
print("Write successful.")
# Check file content externally
except Exception as e:
print(f"Error: {e}")
Conclusion
The TypeError: X takes exactly one argument (N given)
occurs when you violate a method's signature by passing more arguments than it accepts.
- For
list.append()
: It takes exactly one argument (the item to add). To add multiple items individually, uselist.extend(iterable)
instead. To add a collection as a single item, wrap it (e.g.,my_list.append(['c', 'd'])
). - For
file.write()
: It takes exactly one string argument. To write multiple pieces, either concatenate them into a single string first (using+
or f-strings) or make multiple separate calls towrite()
.
Always check the documentation or use help()
for unfamiliar methods to understand the arguments they expect.