TypeError: cannot unpack non-iterable int object

Often when working on a data analytics project it requires you to split data out into its constituent parts.

There are a number of reasons for this, it can be confusing when you get errors as with the title of this post.

Before we explain this error and what it means, lets us first explain unpacking

Unpacking basically means splitting something up into a number of its parts that make it up.

To demonstrate if you take a,b,c = 123, and look to unpack it, it throws out the error, but why?

Well pure and simple, we have three values on the left “a,b,c”, looking for three values on the right.

a,b,c = 123
print(a)

Output:
 a,b,c = 123
TypeError: cannot unpack non-iterable int object

If you would like to fix this problem change the right hand side to have three values.

a,b,c = 1,2,3
print(a)
print(b)
print(c)
print(a,b,c)

Output:
1
2
3
1 2 3

Process finished with exit code 0

In essence, what is going on is that an evaluation checking that both sides have the same amount of values.

It is important to remember, the code above we used to show the error is an integer, which cannot be unpacked.

So if you take 123 for an example, which we used here it cannot be split into say 100 and 10 and 13.

In this case, even though when they are added up to 123, integers cannot be unpacked.

For this reason in the code for our solution, the difference is that the values used are tuples as follows:

a,b,c = 1,2,3
print(a)
print(b)
print(c)
print(a,b,c)
print(type((a,b,c)))

Or 
a,b,c = (1,2,3)
print(a)
print(b)
print(c)
print(a,b,c)
print(type((a,b,c)))

yield the same result:

1
2
3
1 2 3
<class 'tuple'>

Process finished with exit code 0

So in summary:

When unpacking there are a number of things to remember:

  • Integers on their own cannot be unpacked.
  • You need to make sure that if you have a number of variables, that you have the same number of integers if they the values.
    • This will make it a tuple and unpacking can then happen.

ValueError: invalid literal for int() with base 10

Have you been working with the data type int, and getting the above error?

As a data analytics practitioner, this problem pops up quite frequently, so it is time to get to the bottom of it!

Here we explain the data type, why the error occurs and examples of how to fix it.

So what are INT and what are their properties?

INT or integers are whole numbers with are positive or negative. They cannot hold any decimal places.

Also INT can return an integer object from a number or string.

Any value that has a decimal place in it will be rounded down, and anything after the decimal place will be ignored.

For the below you can see the output is rounded down:

data_error = int(1.75)
print(data_error)
print (type(data_error))
output ===> 1
<class 'int'>

How can string inputs be managed?

Strings are read in, and the program will handle the string as an int, and then perfom its checks.

For example:

data_error = int('1')
print(data_error)
print (type(data_error))
Gives output of: 1
<class 'int'>

BUT

data_error = int('1.75')
print(data_error)
print (type(data_error))
Gives output ====> ValueError: invalid literal for int() with base 10: '1.75'

This is exactly the problem we are looking to resolve. When you read the value as an integer without any inverted commas (i.e. 1.75), it treats it as an integer.

On the other hand, when you include the inverted commas ( i.e. ‘1.75’), the program cannot manage the conversion from string and throws the error above.

For reference Base 10 represents all the integers from 0-9, which are whole numbers, in essence, ten possibilities.

It is used to assign numbers to an integer or string passed to an integer, so for example 3569 is:

3 has a place of 3000

5 has a place of 500

6 has a place of 60

and 9 has a place of 9.

Click here to see a full explanation of Base 10 , the error is used to signify that the output does not align with the Base 10 system.

How can I fix this problem when I encounter it in a program?

Returning to this problem below, the solutions that could be applied are as follows:

data_error = int('1.75')
print(data_error)
print (type(data_error))
Gives output ====> ValueError: invalid literal for int() with base 10: '1.75'

Fix1: Remove the inverted commas, giving you:

data_error = int(1.75)
print(data_error)
print (type(data_error))
Output ====> 1
<class 'int'>

Fix2: Only include integer values when passing to another object.

This will help with ensuring the controls are in place to reduce these errors occurring.

Also if you have to use values inside inverted commas , make sure they are of an integer type only, with no decimal places.

To wrap with conclusions

In summary, the best way to avoid this problem is to use integer values at all times, with no decimal places.

If you have to use a value with a decimal place then do not include it inside inverted commas so the computer program can read it correctly, drop everything after the decimal point and just return an integer.

On our YouTube channel you can subscribe to find out more information about ValueErrors and many more tips and tricks to help you with better data analytics.

We have created a video for this blog post, have a look at it, and it you like the channel please subscribe!

We have also got a fix here for value error pattern contains no capture groups