Using a list of lists in a function


#1

When I run this it prints out [1, 2, 3, 4, 5, 6, 7, 8, 9] but I get an error at the bottom saying - flatten([[1, 2], [3, 4]]) returned [1, 2, 3, 4, 5, 6, 7, 8, 9] instead of [1, 2, 3, 4]

Any help would be appreciated.

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]

Add your function here

def flatten(lists):
results = []
for item in n:
for number in item:
results.append(number)

return results

print flatten(n)


#2

Never mind. A minute after I posted this I went back to playing around and got it!

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]

Add your function here

def flatten(lists):
results = []
for item in lists:
for number in item:
results.append(number)

return results

print flatten(n)


#3

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