18. Using a list of lists in a function


#1



https://www.codecademy.com/en/courses/python-beginner-nzzVa/4/2?curriculum_id=4f89dab3d788890003000096#


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]]

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

print flatten(n)


#2

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:) .


#3

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 ?


#4

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


#5

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