# Using Lists of Lists in function

#1

Hello Friends,
I am strucked with the exercise passing of of list of lists as parameters to functions.I did as per the example shown here.But I could not understand the concept of iteration of innerlists and outerlist. My doubt is when the loop varible from innerloop passes control to the outerloop. I request to explain me with a simple example.

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

I am getting the message Oops, try again. flatten([[1, 2], [3, 4]]) returned [1, 2, 3, 4, 5, 6, 7, 8, 9] instead of [1, 2, 3, 4]

#2

Code problem: Your code is correct. But, you have to iterate over the variable `lists` and not `n`.

Logic problem:

Here's a simple example:

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

print "Starting from outside\n"
count_o = 0

for block in lst:
count_o += 1

print "At iteration ", count_o, "of outer loop"
print block

count_i = 0

for number in block:
count_i += 1
print "    At iteration ", count_i, "of inner loop"
print number

print "\n"``````

(Run online)

Output given is:

``````Starting from outside

At iteration  1 of outer loop
[1, 2, 3]
At iteration  1 of inner loop -> 1
At iteration  2 of inner loop -> 2
At iteration  3 of inner loop -> 3

At iteration  2 of outer loop
[4, 5, 6]
At iteration  1 of inner loop -> 4
At iteration  2 of inner loop -> 5
At iteration  3 of inner loop -> 6

At iteration  3 of outer loop
[7, 8, 9]
At iteration  1 of inner loop -> 7
At iteration  2 of inner loop -> 8
At iteration  3 of inner loop -> 9``````

Hopefully, the output is self-explanatory. I hope it helps

#3

@gaurangtandon. Yeah, its was small mistake from my end and i couldnot trace it. I have executed your code and what i undertstand is that, we have to split the list by using two for loops one to print the internal lists and other for loop to print to print the contents of the internal list within the outerloop count varible.Please correct me if i go wrong with my understanding

#4

Yes you sound like you are good with the understanding of these loops and are ready to conquer the rest of the exercises!

#5

Thanks for the timely help. I want to learn python programming language more efficiently and shall hope to do well in rest of the exercises