TypeError: tuple object cannot be interpreted as an integer

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: tuple object cannot be interpreted as an integer #

The Python "TypeError: 'tuple' object cannot be interpreted as an integer" occurs when we pass a tuple to a function that expects an integer argument. To solve the error, either unpack the tuple or access the tuple element at a specific index.

typeerror tuple object cannot be interpreted as an integer

Here are 2 examples of how the error occurs.

main.py
import numpy as np my_tuple = (2, 4) # ⛔️ TypeError: 'tuple' object cannot be interpreted as an integer print(np.random.randn(my_tuple)) # ⛔️ TypeError: 'tuple' object cannot be interpreted as an integer for i in range(my_tuple): print(i)

We have a tuple that contains 2 elements, but the random.randn() and range() functions expect integer arguments instead of a tuple.

If you wrapped the integer arguments in two sets of curly braces, remove one set as this creates a tuple.

One way to solve the error is to access the tuple elements at a specific index if your tuple stores integers.

main.py
import numpy as np my_tuple = (2, 4) # ✅ works print(np.random.randn(my_tuple[0], my_tuple[1])) # ✅ works for i in range(my_tuple[0], my_tuple[1]): print(i)
Our tuple stores integer values, so we can access the item at a specific index and pass that as an argument to the function.

If your tuple stores exactly as many elements as the function expects arguments, you can unpack the tuple in the call to the function.

main.py
import numpy as np my_tuple = (2, 4) # ✅ works print(np.random.randn(*my_tuple)) # ✅ works for i in range(*my_tuple): print(i)

You can imagine that the *my_tuple syntax unpacks the two elements of the tuple and passes them as individual arguments to the function.

We end up passing 2 integers as arguments to the function, instead of a single tuple.

Make sure you aren't declaring a variable that stores an integer initially and overriding it somewhere in your code.

main.py
my_int = 10 my_int = 15, # 👈️ note: the trailing comma creates a tuple print(type(my_int)) # 👉️ <class 'tuple'> # ⛔️ TypeError: 'tuple' object cannot be interpreted as an integer for i in range(my_int): print(i)

We initially set the my_int variable to an integer but later reassigned it to a tuple which caused the error.

Tuples are constructed in multiple ways:

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

Conclusion #

The Python "TypeError: 'tuple' object cannot be interpreted as an integer" occurs when we pass a tuple to a function that expects an integer argument. To solve the error, either unpack the tuple or access the tuple element at a specific 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.