Iterating over a list in a function = ([[1, 2], [3, 4]]) returned [[1, 2], [3, 4]] instead of [1, 2, 3, 4]


#1

> n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
> # Add your function here

> def flatten(lists):
>     results = []
>     for number in lists:
>         results.append(number)
>     return results

> print flatten(n)

Oops, try again. flatten([[1, 2], [3, 4]]) returned [[1, 2], [3, 4]] instead of [1, 2, 3, 4]

I know its kinda silly and I could copy someone else code to pass it, but I want to learn so I need to know what am I doing wrong right here.Obviously I dont have an idea how to add one list to another on this code.

Cheers.


#2

for number in lists:
         results.append(number)

When you iterate ,the iteration variable takes value of items of lists which are list only,and you end up returning same list (same as input's) when you append them to results
To access individual items of items of lists ,You need to use another for loop.

hint**

some_list = [[1,2,3,4]]

for i in some_list:
    for j in i:
        print j

It will print..

1
2
3
4

#3

So you basically mean I have to add one more loop or replace the loop I've made?

1 of the mission objectives is to "Iterate through lists. Call the looping variable numbers."
So I cant replace 'numbers', and 2 loops on a same variables in one function seems a bit weird, I dont feel it.

Plus, I dont understand the hint, "i" stands for item, but "j" ?


#4

j is internal loop's looping variable.

when a list contains other lists like this..

a_list = [['a','b'] ,['c','d']]
and when you loop over it..

for i in a_list:
    print i

what it will print?
items of a_list...

['a', 'b']
['c', 'd']

but you wanted to append all the items of individual lists inside a single list?
so we needed to add a second for loop that'd loop over internal items of individual lists.

we can do this ..

for internal_list in a_list:
    for item in internal_list:
        print item

what it will print now?

a
b
c
d

Also in the current exercise You have to append them (to your empty list that you've created) rather print them (As its just a hint)
do reply if it made your doubt clear or not?


#5

if isn't working for me still.

n = [[1, 2], [3, 4]]

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

print flatten(n)

it gives me an error:

Oops, try again. flatten([['a', 'b'], ['c', 'd'], ['e', 'f']]) returned [1, 2, 3, 4] instead of ['a', 'b', 'c', 'd', 'e', 'f']

I don't know what I'm doing wrong


#6

You could have created a new post ?

You're making a mistake in first for loop.
Your parameter is lists but youre iterating over n?why ?


#7

I didn't see that. Thanks. IDKW the lists was on there because I originally put N there. Must of changed it by accident


#8

I've just did some infinite loop that hanged my browser.■■■■ I'm good, such a potential.
Yet I cant get that to work somehow.

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

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

Oops, try again. flatten([[1, 2], [3, 4]]) returned [[3, 4]] instead of [1, 2, 3, 4]

Tried different syntax, gives just different errors.
Im just a simple useless humanist, what am I even doing here.

:no_mouth:


#9

Python like other languages does not depend on curly braces but it uses spaces to structure its different blocks of codes.
More precisely these spaces are called "Indentation".
In your above code your indentation is wrong.
also you did not needed to print this..
print j
(If you read my last post I have written this...

Also in the current exercise You have to append them (to your empty list that you've created) rather print them (As its just a hint)

)

also this line should be inside the second for loop.
results.append(numbers)
also you have to append j into it?

not numbers?

I will make your work easy by giving you a syntax..

In your second for loop..

for j in numbers:
     # you don't need to print **j**
     #your append should be here can you see how I've indented it?    
     #tho It will automatically indented by CC's interpreter.

I hope it helps!


#10

Ah ok, I got this.
I tried to remove that "print j" earlier but it was some syntax error without it so I went lazy way and left it there.

Seems the key was appending "j" instead of printing it.

Ty very much, your help done the hardest job in this task.


#11

good work!!!cheers!! :thumbsup:


#12

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