# 11/18 List manipulation in functions

#1

Hi to all.... I don´t understand What I doing wrong?...

``````n = [3, 5, 7]
def list_extender(lst):
lst.append(9)
return n
print list_extender(n)``````

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

Then, accidentally I changed the value of return from n to lst... and code is right...Why?

``````n = [3, 5, 7]
def list_extender(lst):
lst.append(9)
return lst
print list_extender(n)``````

Why if both codes print the same result? [3, 5, 7, 9]

#2

@mrugerio

Hi!
In the first block of code you have, you are `return`ing n but you need to be return the parameter of your function which is `lst` then, when you call `list_extender` you need to use `n`(the array) as your parameter when calling

#3

because in your first block of code, you're returning n from the Global variables (variables created outside the function's scope), since n doesn't exist as a Local scope, it searches in the global scope.

the variable lst is created inside the `list_extender` function, and only exists while this function is running,thus a Local variable, once you exit this function, the variable lst is destroy, which is why it needs to be return-ed.

lst takes it's input from n, so it makes a copy inside the function(in the local scope), it appends 9 to the end of the list, making it

``````Global variable: n = [3, 5, 7]
Local variable: lst =  [3, 5, 7, 9]``````

list_extender(n) returns the lst from the function, and print takes the result and prints it out to console.

Hope this helps.

#4

thanks...really helped me..

#5

How does lst take its input from n if n is outside the function? What is connecting these two things together at the point that you "return lst"?

#7

This just worked for me:

#8

Hi cone11 did you ever find the answer to this? I'm still a bit lost on what connects the two variables. How does it know that n has anything to do with lst?

#9

Just thought I would post this in case anyone else ever has the same error as me. My error related to this question was simply a syntax one, I misread lst and thought it said first (1st). Simple error on my part, but gave me a good while of frustration. A solid noob mistake ^_^; but I thought I'd post it in case anyone else has the same issue

#10

jesus )(*@#\$^@#\$%^ christ. I have literally been banging my head against my keyboard trying to figure this out. This is the second or third time some stupid misread has caused me major headaches.

Thanks for clearing that up though

#11

I managed to work this one out pretty quickly; however, it still took me ages!! I read it as 1st rather than lst. Nightmare!!!!!

#12

And why this is not working

#13

Hey ayaz, I actually did that error the first time. It's actually asking you to use a variable called lst, as in last, not one s t. So LST lowercase. Hope that helps.

#14

You are superman archosuchus thanks

#15

Champ!!! That was annoying to realize! Some of these tutorials are really ambiguous!

#16

n = [3, 5, 7]

def list_extender(lst):
lst.append(9)
return lst

print list_extender(n)

this worked for me

#17

Because you are passing a list n (which has already been created outside the function and hence has global scope). Now if you pass n to the function, the function receives it via a parameter x.And now x has the same list has n. You can use x to modify the list and return it (within the function).

#18

This worked for me:

n = [3, 5, 7]
def list_extender(lst):
list_extender = lst
list_extender.append(9)
return list_extender

print list_extender(n)

#19

A post was split to a new topic: Argument vs parameter

#20

thanks everyone this helped me a lot!!!

#21