Last updated: Apr 8, 2024
Reading timeยท5 min
The Python "TypeError: tuple expected at most 1 argument, got 2" occurs when
we pass multiple arguments to the tuple()
class.
To solve the error, pass an iterable, such as a list containing the arguments.
Here is an example of how the error occurs.
# โ๏ธ TypeError: tuple expected at most 1 argument, got 2 my_tuple = tuple('a', 'b')
The tuple class takes at most 1 argument - an iterable.
We can pass multiple values in a list to the tuple class.
my_tuple = tuple(['a', 'b']) print(my_tuple) # ๐๏ธ ('a', 'b') print(my_tuple[0]) # ๐๏ธ 'a' print(my_tuple[1]) # ๐๏ธ 'b'
Python indexes are zero-based, so the first item in a tuple has an index of 0
,
and the last item has an index of -1
or len(my_tuple) - 1
.
If you need to create a tuple containing lists as items, pass a two-dimensional list to the tuple() class.
my_tuple = tuple([['a'], ['b']]) print(my_tuple) # ๐๏ธ (['a'], ['b'])
Tuples are constructed in multiple ways:
()
creates an empty tuplea,
or (a,)
a, b
or (a, b)
tuple()
constructorNote that tuples are immutable, so if you have to mutate a sequence, you have to
use a list
instead.
my_list = ['a', 'b', 'c'] print(my_list) # ๐๏ธ ['a', 'b', 'c'] my_list.append('d') print(my_list) # ๐๏ธ ['a', 'b', 'c', 'd'] my_list.insert(0, 'z') print(my_list) # ๐๏ธ ['z', 'a', 'b', 'c', 'd'] print(my_list[0]) # ๐ 'z'
Note that lists are created using square brackets.
The list.append() method adds an item to the end of the list.
The method returns None as it mutates the original list.
If you need to add an item at a specific index, use the insert()
method.
The list.insert()
method inserts an item at a given position.
The method takes the following 2 parameters:
Name | Description |
---|---|
index | The index of the element before which to insert |
item | The item to be inserted at the given index |
The Python "TypeError: input expected at most 1 argument, got 3" occurs when
we pass multiple arguments to the built-in input()
function.
To solve the error, use a formatted string literal to concatenate the arguments into a single string.
Here is an example of how the error occurs.
w = 'name' # โ๏ธ TypeError: input expected at most 1 argument, got 3 s = input('Your ', w, ': ')
input()
built-in function expects at most 1
argument, so we have to concatenate the arguments into a single string.One way to solve the error is to use the addition (+) operator to concatenate the strings.
w = 'name' s = input('Your ' + w + ': ') print(s)
The addition (+) operator can be used to concatenate strings.
print('a' + 'b' + 'c') # ๐๏ธ 'abc'
An alternative approach is to use a formatted string literal.
w = 'name' s = input(f'Your {w}: ') print(s)
f
.Make sure to wrap expressions in curly braces - {expression}
.
This solves the error because we are passing a single, string argument to the
input()
function.
The input() function takes an optional prompt
argument and writes it to standard output without a trailing newline.
s = input('Your name: ') print(s)
The function then reads the line from the input, converts it to a string and returns the result.
An alternative to providing the prompt
argument is to use calls to the
print()
function.
print('Your name: ') s = input() print(s)
This is very similar to passing the prompt
argument to the input()
function
but instead of showing the prompt on the same line, it is displayed on a
separate line.
The Python "TypeError: list expected at most 1 argument, got 2" occurs when we
pass multiple arguments to the list()
class which takes at most 1 argument.
To solve the error, use the range()
class to create a range object or pass
an iterable to the list()
class.
Here is an example of how the error occurs.
# โ๏ธ TypeError: list expected at most 1 argument, got 2 my_list = list(0, 5)
The list()
class expects to get called with at most 1 argument - an iterable.
my_tuple = ('a', 'b', 'c') my_list = list(my_tuple) print(my_list) # ๐๏ธ ['a', 'b', 'c']
If you meant to create a range of integers, use the range()
class.
my_list = list(range(0, 5)) print(my_list) # ๐๏ธ [0, 1, 2, 3, 4]
And if you meant to create a list containing multiple items, pass the items in
an iterable in the call to the list()
class.
my_list = list(['a', 'b', 'c']) print(my_list) # ๐๏ธ ['a', 'b', 'c']
The list class takes an iterable and returns a list object.
The range() class is commonly used for looping a specific number of times in for loops and takes the following parameters:
Name | Description |
---|---|
start | An integer representing the start of the range (defaults to 0 ) |
stop | Go up to, but not including the provided integer |
step | Range will consist of every N numbers from start to stop (defaults to 1 ) |
If you only pass a single argument to the range()
constructor, it is
considered to be the value for the stop
parameter.
for n in range(5): print(n) result = list(range(5)) # ๐๏ธ [0, 1, 2, 3, 4] print(result)
start
argument is omitted, it defaults to 0
and if the step
argument is omitted, it defaults to 1
.If values for the start
and stop
parameters are provided, the start
value
is inclusive, whereas the stop
value is exclusive.
result = list(range(1, 5)) # ๐๏ธ [1, 2, 3, 4] print(result)
If the value for the stop
parameter is lower than the value for the start
parameter, the range will be empty.
result = list(range(1, 0)) # ๐๏ธ [] print(result)
You can learn more about the related topics by checking out the following tutorials: