TypeError: 'int' object is not iterable, why it's happening

int, iterable-unpacking, python

Solution

In-place addition on a list object extends the list with the elements of the iterable. `k*k` isn't an iterable, so you can't really "add" it to a list.

You need to make `k*k` an iterable:

result += [k*k]

Problem

Here is three examples actually. ``` >>> result = [] >>> for k in range(10): >>> result += k*k >>> result = [] >>> for k in range(10): >>> result.append(k*k) >>> result = [k*k for k in range(10)] ``` First one makes a error. Error prints like below ``` TypeError: 'int' object is not iterable ``` However, second and third one works well. I could not understand the difference between those three statements.

Original source

Related problems