What is the problem in my result?


#1



https://www.codecademy.com/en/courses/python-beginner-nzzVa/4/2?curriculum_id=4f89dab3d788890003000096#

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

I am passing n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]] into my flatten function, it returns [1, 2, 3, 4, 5, 6, 7, 8, 9], which is the expected output.


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

results = []

def flatten(lists):
    
    for numbers in lists:
        for i in range(numbers[0],numbers[0]+len(numbers)):
            results.append(i)
    return (results)
    
a = flatten(n)
print a


#2

This is outside the function. Therefore whatever you add to it will not be cleared every time you call the function. Hence, it will have everything flattened in it (your n list and their testing list, [[1, 2], [3, 4]]).


#3

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