Why does using another method of iteration give me error?


#1



for 18. Using a list of lists in Function


Why does python tell me:

Traceback (most recent call last):
File "python", line 10, in
File "python", line 6, in flatten
TypeError: 'int' object is not iterable

if I write:

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
    results = []
    for numbers in range(len(lists)):
            for i in numbers:
                results.append(i)
    return results

print flatten(n)

?

I realize that the following is the correct answer but I would like to know what makes the former wrong.
...
for numbers in range(len(lists)):
for i in numbers:
results.append(i)
return results

`


#2

using range() will give you numbers:

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
for numbers in range(len(n)):
    print numbers

you can use this numbers as indexes to get values from n:

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
for numbers in range(len(n)):
    print n[numbers]

then you are good

here:

for i in numbers

you used numbers, which are integers (due to range(), which is why you got the error


#3

I understand now. Thank you!


#4