Using a list of lists in a function


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

Add your function here

def flatten(lists):
results = []
for numbers in lists:
return numbers
for o in numbers:
return results

print flatten(n)

Please I don't know what I'm doing wrong.

Replace this line with your code.


You have a function call flatten and you are printing it at the end instead of calling it.
To call a function just simply call its name without anything before, like this:


@syntaxity ,

You should format code when you post it, so that we can see important details, including indentation, See How do I format code in my posts?.

You have two return statements. It might be that the first one ...

return numbers

... terminates execution of the function, preventing it from completing its task.

Also note that here you are attempting to use o as an index ...


Instead, append it directly to results


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

Add your function here

def flatten(lists):
|___results = []
|___|___for numbers in range(len(lists)):
|___|___|___for i in lists[numbers]:
|___return results
print flatten(n)


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

This is what worked for me.
For this, I figured I needed to have create a for loop of the indexes for the list and then another for loop to use those indexes to take the data from the lists inside of the list. Then, you .append() the data to your results list. I am still new to coding, but that is how I came to my answer. If I am wrong in my process, please let me know, but it got me the answer I needed. I have also taken the liberty of showing the indentations in detail.


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