Anyone could help me to identify the problem?


#1

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

Add your function here

def flatten(lists):
results = []
for numbers in range(len(lists)):
results = results + results.append(lists[numbers])
return results

print flatten(n)

I don't konw why there are two errors.


#2

What errors is it giving you?


#3

Traceback (most recent call last):
File "python", line 10, in
File "python", line 6, in flatten
TypeError: can only concatenate list (not "NoneType") to list


#4

I dont know why I pasted it and then the format is wrong in the discuss forum.


#5

See if this works.

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

I think you've got to use 2 for loops since you're using numbers inside lists inside a directory.


#6

I think it should be below instead of results.append(i), but each of them don't works.
it is a little hard for using tow for.

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

print flatten(n)


#7

I couldn't get it to work with results.append(numbers[i]), had do use results.append(i). Also change range(len(lists)): to just lists: and see if that does it.


#8

If use for x in lists instead of for x in range(), that is easy. just refer to below.
In the first time I use for x in range(), then I'd find out how can it work.

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

print flatten(n)


#9

@ghughes13

Could you please help me with this. I just need to understand the working of below code.

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
    results = []
    for numbers in range(len(lists)): #The length o f "n" is 2 and so the iterations
        results.append(numbers) #This should take the first list [1,2,3] and append it to results.Similarly for second iteration it would append [4,5,6,7,8,9]
        return results
print flatten(n)

Outputs: [0]


Doubt:

I have added my understandings as comments in above code. So as per my logic it should output [1,2,3,4,5,6,7,8,9]. Then why does it actually outputs [0]?


#10

You are iterating over a range which is 0-what ever. You then populate a list with the first item which happens to be 0.

Finally you return immediately the list after the first iteration, resulting in a list of [0]


#11

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
    results = []
    for numbers in range(len(lists)): #The length o f "n" is 2 and so the iterations
        results.append(numbers) #This should take the first list [1,2,3] and append it to results.Similarly for second iteration it would append [4,5,6,7,8,9]
    return results
print flatten(n)

I did a little edit, but the output is still not match the expectation, How to edit it?


#12

i am going to slightly modify your code, take a look at it:

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

i changed what is inside your for loop. as you can see, because you use range, they will be simple numbers (which correspond with the indexes). so using lists[numbers] will actually give you the nested lists, so you are going to need another for loop inside the current for loop, to get the numbers from the nested lists.

Keep in mind that if you use range, you will get the indexes, not the numbers in the lists


#13

you are so kind, thank you.
just keep learning hard for the code.