# Modifying each element in a list in a function (SOLUTION)

#1

My code does not return what I need from the function, here is the error

Oops, try again. double_list([0, 1]) returned [12, 5, 7] instead of [0, 2]

Instead it should just let me proceed, by doubling each key in the list

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

# Don't forget to return your new list!

print double_list(n)``````

#2

Okay...

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

def double_list(x):
for i in range(0, len(x)):
x[i] = x[i] * 2
return x # Don't forget to return your new list!
print double_list(n)``````

That's what I ended up with.

#3

the whitespace on your return is incorrect

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

The way you currently had it formatted would return on the first iteration of the for.
White space is important.

#4

Yep, that's what I was basically saying...though is there a difference between having n and x in for?

#5

I was maintaining his original code.

Generally speaking you should be using x throughout the function, not an outside reference to 'n'.
That wasn't his question, so I left it alone.
You are correct, though.

#6

Thanks for your reply it helped, also @sw0rdsdance thanks for pointing out the topic about the 'x' and the 'n' , i just fixed it and it worked, here is the final code

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

# Don't forget to return your new list!

print double_list(n)``````

#7

Yay~! Don't forget to mark the post as a solution.

#8

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