# How do I change element in a list in a function?

I got this code:

``````def update(ll, n, v):
[ll, n, v][1] = v
return [ll, n, v]

m = [1, 2, 3, 4]
m = update(m, 2, 'new')
print(m)

``````

What I want to do is replace n with the information v, so the list print should say:

`[[1, 2, 3, 4], 'new', 'new']`

I found out that you could replace element in list with [ll, n, v][1] = v but it doesn’t seem to work.

If you’re just typing it interactively:

``````>>> lst = [1,2,3,4]
>>> lst[2] = 0
>>> lst
[1, 2, 0, 4]

``````

You don’t necessarily need a function, but you could put that into one if you wanted… (with added functionality?)

But I want to have a function or more I want to learn how to do it with a function or with this method.

Just apply the same idea:

``````test = [1,2,3,4]

def change_list(lst, i, newValue):
lst[i] = newValue
return lst

changed_list = change_list(test, 2, 150)
print(changed_list)
#[1, 2, 150, 4]
``````

``````def update(ll, n, v):
[ll, n, v][1] = v
return [ll, n, v]
``````

is saying,

• for a list of three args: [ll, n, v], set index-1 to v
• return a list of three args [ll, n, v] (regardless of what happens in the previous line)

So although it’s called update, what it really does is return a list of 3 arguments you provide. e.g if you invoke update(2,3,4) it will return [2,3,4] etc.

2 Likes

The result is wrong, what I want to replace is n with v and in your case i with newvalue.

The original list is: [[1, 2, 3, 4], 2, 150]
Should be [[1, 2, 3, 4], 150, 150]

But you only print: [1, 2, 150, 4]

I just gave an example, i is placeholder for i’th item. The point is that when you get the hang for the how the methods interact you can go to town as you please in terms of their functionality.

It seems like the function you want makes a new list with n of some new value.

``````def make_new_list(old_value, n, new_value):
new_list = [old_value]
for i in range(n):
new_list.append(new_value)
return new_list

test = ["playing", "with", "lists"]
print(make_new_list(test, 3, "experimentation is best"))
``````

output: `[['playing', 'with', 'lists'], 'experimentation is best', 'experimentation is best', 'experimentation is best']`

Also note I changed the name of the function, since we are not really changing the original list as much as creating a new one.

1 Like

Thank you for taking your time helping me but now after 2 days I’m still clueless. I understand what wrong I did but I don’t know which command to use to fix it. I understand that by calling the return that way in the function, the changes doesn’t happen. But how should I call it then? I see you are giving alot of examples and another guy before you also did but I don’t want to do it that way. No for in range or no examples without function.

Here is the original code and the task I got was to find what is wrong in the code and fix it:

``````def update(ll, n, v):
ll[n] = v
return v

m = [1, 2, 3, 4]
m = update(m, 2, 'new')
print(m)
``````

When the code is fixed the print should be:

``````[[1, 2, 3, 4], 'new', 'new']
``````

I found out that this code:

``````def update(ll, n, v):
return [ll, v, v]

m = [1, 2, 3, 4]
m = update(m, 2, 'new')
print(m)

``````

Gave the correct print:

``````[[1, 2, 3, 4], 'new', 'new']
``````

But then the code is not really performing a replace, it is not really replacing n with v but just printing out v two times. So what I’m really asking is what command do I use in the function to replace n with v?

Reconsider using a for-loop. It’s a very common and powerful technique.