18/18 Using a list of lists in a function?


#1

Please correct me

my code for this section:

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
    results = []
    for numbers in lists:
        for i in numbers:
            results.append(i)
        return results

print flatten(n)

Link: https://www.codecademy.com/courses/python-beginner-nzzVa/4/2?curriculum_id=4f89dab3d788890003000096
The error: flatten([[1, 2], [3, 4]]) returned [1, 2] instead of [1, 2, 3, 4]

I don't understand why I wrong? Please explain to me?


#2

The return should not be inside the loop.


#3

Move the return statement out one indention. It's returning after one iteration of the first for loop and needs to return after the loop is completely finished.


#4

many thank :slight_smile: I have finished this section :slight_smile:


#5

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