18.Using a list of lists in function


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/python-beginner-nzzVa/4/2?curriculum_id=4f89dab3d788890003000096

<In what way does your code behave incorrectly? Include ALL error messages.>
Oops, try again. flatten([[1, 2], [3, 4]]) returned [1, 2] instead of [1, 2, 3, 4]

<What do you expect to happen instead?>
[1,2,3,4,5,6,7,8,9]

```python

Replace this line with your code.
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
results = []
for numbers in lists:
for item in numbers:
results.append(item)
return results
print flatten(n)

<do not remove the three backticks above>

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

#2

The return statement should be outside of for loop.


#3

In Python indentation is VERY import.

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
    #start of FUNCTION, 4-spaces indent
    results = []  # define an empty =list=
    for a_list in lists:
        # 1st FOR loop, an extra 4-spaces-indent
        for item in a_list:
            # 2nd FOR loop, an extra 4-spaces-indent
            results.append(item)
        # END of 2nd FOR loop
    # END of 1st FOR loop
    # now RETURN the =results= list
    return results
#END of FUNCTION

print flatten(n)

#4

Thanks for all ! You are awesome


#5

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