TypeError: ‘NoneType’ object is not iterable

Estimated reading time: 2 minutes

Have you wondered why you cant iterate over a NoneType but never had it properly explained? Read on as here we will fully explain the concept and how you can fix it.

So what is a NoneType anyway?

First of all, to understand a none type, you need to understand what none is.

None is essentially no value, so in other words, nothing is present. It differs from the integer zero, as the integer zero is an actual value and it exists.

If you were to put both of them side by side on a database table, one would be empty the other would have a value of zero.

Also, None is a type in itself:

a = None
print(a)
print(type(a))

Gives the output:

None
<class 'NoneType'>

Whereas zero is an integer datatype:

a = 0
print(a)
print(type(a))

Giving output:
0
<class 'int'>

So how does a none type object is not iterable occur?

Let’s take an example and walk through it. In the below code we have a variable that is empty. In other words, we are asking the program to loop through something that does not exist and does not have any values.

a = None
print(a)
print(type(a))

for i in a:
   print(i)

Giving output:
for i in a:
TypeError: 'NoneType' object is not iterable
None
<class 'NoneType'>

Repeating what we said above, it is not possible to loop through something that does not have a value.

But if we change it to a string value of ‘1’, the loop will work.

a = '1'
print(type(a))

for i in a:
   print(i)

So in summary:

(A) NoneTypes are useful if you just want to return an empty value or check for an empty value.

(B) You cannot loop over NoneTypes as they do not have any values, they are empty!

TypeError: type object is not subscriptable

Estimated reading time: 2 minutes

I was recently working on our last blog post how to reverse a string in python and I came across this error.

The thought passed me what does it mean and how can I fix it?

So what does the error actually mean?

Essentially it means that , you are trying to access an object type, that has a property of “type”.

What are typical property of types? Well they can be:

  • int()
  • str()
  • tuple()
  • dict()

The above allows you to change your data to these data types, so the data contained within them can be further manipulated.

In essence you are trying to call type in the wrong way and in the wrong place in your code.

By calling it , it will throw this error, and they should be avoided, as they are a built in function.

Lets take an example of how we can replicate this error and fix it

name1 = "joe" # These have index values of [0,1,2]
emptylist =[]
strlength = len(name1) # Returns length of three
while strlength > 0:
    emptylist += str[strlength - 1] #This is the last index value of the variable "name1"
    strlength = strlength - 1
print(emptylist)

In the above code all appears well, but in line 5 the “str” before the [ is the problem. The code automatically looks to call this function.

The simple answer to fixing this is to rename it to name1 as follows:

name1 = "joe" # These have index values of [0,1,2]
emptylist =[]
strlength = len(name1) # Returns length of three
while strlength > 0:
    emptylist += name1[strlength - 1] #This is the last index value of the variable "name1"
    strlength = strlength - 1
print(emptylist)

which gives you the following error free output:

Result with no error: ['e', 'o', 'j']

In summary and what not to do

So it is clear that referencing types as a string variable should be avoided, and keep your code clean from this perspective.

This would also apply to any reserved words in Python as well.

Have you seen type error list object is not an iterator?

Also you may have come across type error float object is not callable