Solve - TypeError: tuple expected at most 1 argument, got 2

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

Solve - TypeError: tuple expected at most 1 argument, got 2 #

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, e.g. my_tuple = tuple(['a', 'b']).

typeerror tuple expected at most 1 argument got 2

Here is an example of how the error occurs.

main.py
# ⛔️ 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.

main.py
my_tuple = tuple(['a', 'b']) print(my_tuple) # 👉️ ('a', 'b') print(my_tuple[0]) # 👉️ 'a' print(my_tuple[1]) # 👉️ 'b'

If you need to create a tuple containing lists as items, pass a two-dimensional list to the tuple() class.

main.py
my_tuple = tuple([['a'], ['b']]) print(my_tuple) # 👉️ (['a'], ['b'])
Tuples are very similar to lists, but implement fewer built-in methods and are immutable (cannot be changed).

Tuples are constructed in multiple ways:

  • Using a pair of parenthesis () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

Note that tuples are immutable, so if you have to mutate a sequence, you have to use a list instead.

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

NameDescription
indexThe index of the element before which to insert
itemThe item to be inserted at the given index
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.