0 points
Submitted by Abdullah Hamid
over 7 years

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

For this problem you have to concatenates two lists.

This works for me:

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

print flatten(n)``````

This however does not:

``````n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
results = []
for i in range(len(lists)):
for j in range(len(i)):
results.append(j)
return results

print flatten(n)``````

Any pointers on why it doesn’t?

``````n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten (lists):
result = []
for i in range(len(lists)):
for j in range(len(lists[i])):
result.append(lists[i][j])
return result
print flatten(n)``````

That code works correctly. But i think 18/18 is stupid lesson,because no matter what the output, and how you did it. Very bad code checking for validity of the task.

points
over 7 years

I agree

i agree also

I used knowledge from Lists and Functions (17/18) to write a function for (18/18):

``````def flatten(lists):
results = []
for numbers in lists:
results += numbers
return results``````
points
Submitted by Bam Bino

MoonCobbler over 6 years

BLOODY BRILLIANT!!!!!

Rwema over 6 years

Nice

sorry, im confused. how does this work if you have not iterated through numbers yet? which the instructions tell you to do

Thanks–these instructions cause the code to break for me:

For each number, .append() it to results.

Just adding it using += worked fine. I think this particular module was a bit buggy…

This solution works for me:

``````def flatten(lists):
results = []
for numbers in lists:
for item in numbers:
results.append(item)
return results``````

print flatten(n)

points
Submitted by penguin444
over 7 years

penguin444 over 7 years

“return results” should be indented exactly at the same position as “for numbers in lists: “ .

coldcrushforce over 6 years

I see so the numbers refers to the distinct lists inside the dictionary, and “items” is referring to the numbers in each list. thanks your solution helped me understand this exercise.

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

print flatten(n)

points
Submitted by Maria
over 6 years