Why did the len() format not work for the outer loop


#1

I had this same problem but I figured it out but I didnt understand why, please help me.
Why did the len() format not work for the outer loop ?


18. Using List of Lists in a Function by Method 2 only
#2

I did this before

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 nos in numbers :
results.append(nos)

return results

print flatten(n)

I got this error

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


#3

The outer loop in your code is iterating a range. That means numbers is an integer, not a list. To access the inner list in the inner loop, we need to refer to it by index...

for numbers in range(len(lists)):
    for nos in lists[numbers]:

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.