# Using a list of lists in a function

#1

Using a list of lists in a function

Oops, try again. flatten([[1, 2], [3, 4]]) returned [1, 2, 3, 4, 5, 6, 7, 8, 9] instead of [1, 2, 3, 4]

What am I doing wrong?

``````n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
results = []
for lists in n:
for numbers in lists:
results.append(numbers)
return results

print flatten(n)``````

#2

problem1
`for lists in n:`
You need to change one thing here!

problem2.
After you fix problem1, You have to fix this too..
`for numbers in lists:`

hint**
you're looping over input lists? right?
One more thing,when you write a function,You write for general problems of same nature not for specific case!

#3

Thanks for the quick reply.

Changed code to below and it worked:

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

Kept general phrases only. I think I understand why this is correct now.

#4

This is the correct code:

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

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

print flatten(n)

#5

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