TypeError: ‘int’ object is not iterable

For all noobs like me. If you get this error, you most probably doing one stupid thing like I did:

y2 = []
for x in y2.shape[0]:
    y2.append(x[0])

where y2.shape[0] is just an integer. Remember, we can iterate only through lists or other sequences. When we would like to repeat a certain action X number of times we should do the following:

y2 = []
for x in range(y2.shape[0]):
    y2.append(x[0])

If you, let’s say, have an array ‘a’ and would like to iterate through, then:

for i in a[:,0]: 
print(i)

Would actually print all the items in the array first column (: -from start to end, 0 -first column)

This:

for i in len(a[:,0]):
print(i)

Will result in an error. But this will actually work as expected:

for i in range(len(a[:,0])):
print(i)

Because range(len(a[:,0])) is an object: range(0, 96).

So please be careful here, haha! 🙂

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x