18. Using a list of lists in a function



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

is the problem here the (num) i used ? please help

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

def flatten(lists):
    for numbers in lists:
        for num in numbers:
    return results        

print flatten(n)


There is just one problem and It has a valid reason.
Look at these observations of mine ...

Your code

Now look at the excercise instructions---

Instruction actually telling you to make 'Local scoped' list.

You will ask that this code is working fine on your Python IDLE?
Yes, It does.

If you run this code on your python IDLE.
It works fine!

What's the problem ?
The real thing is : global variable introduces more bugs in program .
Codeacademy just trying to teach 'good practice' of programming, otherwise your code works fine.

I hope you know what to do next?:slight_smile:

Wanna read about Global variable and its uses?
I'm global variable and I'm the Villain (Jk :smiley:) .


Thanks man it worked ! :smiley:
tell me if i get it right or wrong , a global variable or a global list is a list outside a function or what ?


Global variable is a wide term , when we say Global list ,it simply means global list variable. :slight_smile:


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