# Modifying each element in a list(lesson) not working!

#1

n = [3, 5, 7]

double_list(x)
for i in range(0, len(n)):
x[i] = x[i] * 2
print n
print double_list(n)

I dont know what I did wrong Its saying this

Traceback (most recent call last):
File "python", line 3, in
NameError: name 'double_list' is not defined

#2

To define a function, we need the `def` keyword:

``````def double_list(x):
for i in range(len(x)):
x[i] *= 2

n = [3,5,7]
double_list(n)
print n         # [ 6, 10, 14 ]``````

#3

my script worked fine. take a look at mine:

n = [3, 5, 7]

def double_list(x):
out = []
for i in range(0, len(x)):
out.append(x[i] * 2)
return out

print double_list(n)

#4

Thanks that helped

#5

Notice the difference between @bosnia_builder 's solution and mine?

Their's creates a new list object and appends the computed values, then returns it. The orginal list is unchanged.

Mine, on the other hand, modifies the list in place, so the old list is lost.

This is not a question of which is right, or which is better, only a demonstration of both approaches. If we need to preserve the original list, then a new object must be created to contain the new list. If not, we can take the shorter route and work with the one list object. It's a situational decision, not a best practice consideration.

#6

Mine is not working too i don t know where the code is wrong

n = [3, 5, 7]
def double_list(x):
for i in range(0, len(x)):
x[i] = x[i] * 2

# Don't forget to return your new list!

``    return double_list(x)``

print double_list(n)

#7

Just remove the n = [3,5,7] line, then it should work.

#8

Its really hard to tell where you went wrong without the correct formatting.

Putting 3 of this symbol ` before and after your code will correctly format it. From what I can tell you returned the list outside of the function. Here is the correct code for reference.

``````n = [3, 5, 7]

def double_list(x):
for i in range(0, len(x)):
x[i] *= 2
return x

print double_list(n)``````

Hope this helps.
FYI
`x[i] = x[i] * 2`
is the same as
`x[i] *= 2`

#9

Sorry, but you don't have return in your code, so it's not functional to pass to next lesson.

#10

It's not meant as a solution, but an example. It illustrates that a reference object that is passed to a function does not get copied into the same scope, but remains in its own scope. That is why we can print n at the end and see the new values in the array.

#11

Can some one please explain what return x does in this function?