Using a list of lists in a function


#1

Please help

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


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




print flatten(n)


#2

The return should be outside of both loops.


#3

Thanks for answer but i got this
AttributeError: 'NoneType' object has no attribute 'append'
with code like this
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
results = []
for numbers in lists:
for item in numbers:
results = results.append(item)

return results 


    # Add your function here

print flatten(n)


#4

sorry i don't know how to paste code correctly, but i just wanted to say that i have this error "AttributeError: 'NoneType' object has no attribute 'append'" when i fixed "return" part


#5

Oh i just forgot to add another = in results = results.append(item)
it works now, thank you mtf


#6

This is a complete statement. We do not assign it to anything.


#8

That was brilliant answer.


#9

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