Using a list of lists in a function


#1

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:
results.append(numbers[o])
return results

print flatten(n)

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


Replace this line with your code.


#2

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:
flatten(n)


#3

@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 ...

results.append(numbers[o])

Instead, append it directly to results


#4

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]:
|___|___|___|___results.append(i)
|___return results
print flatten(n)


[output]

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


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.


#5

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