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?

Answer 5334675c8c1ccc54c8002429

14 votes

Permalink

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

2 comments

Florin about 7 years

I agree

Muhammad YAHYA over 6 years

i agree also

Answer 53fd9f2952f863ab9100130d

3 votes

Permalink

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
about 7 years

4 comments

MoonCobbler over 6 years

BLOODY BRILLIANT!!!!!

Rwema over 6 years

Nice

spiritdriven7 about 6 years

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

Kyle Cyree about 6 years

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…

Answer 531c85c29c4e9db972005144

0 votes

Permalink

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

2 comments

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.

Answer 550b45619113cba761000a5b

0 votes

Permalink

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

Answer 55a4610076b8fe685a00066f

0 votes

Permalink

I believe it is because in the question “Iterate through lists. Call the looping variable numbers.” it asks you to call the looping variable “numbers” for the first looping variable.

points
Submitted by Antonio Scott
about 6 years