18. Why won't my code work?


#1


  1. using a list of lists in a function

Oops, try again. flatten([[1, 2], [3, 4]]) returned [[1, 2], [1, 2]] instead of [1, 2, 3, 4]


n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here

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



print flatten(n)


#2

You're appending the wrong thing. You want to append the items you are iterating through in numbers, not numbers itself. Also, your return has an indentation issue.


#3

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

Add your function here

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

print flatten(n)

thanks that worked.


#4

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