Modifying each element in a list in a function


Why is this not the right answer?

I get this error: Oops, try again. double_list([0, 1]) returned 2 instead of [0, 2]

And in console i get: [6, 10, 14], which is correct.

Thanks for your help in advance.

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

print n


You're not returning n which is what the error message is about.
Hint: try to use the return command instead of print. :slight_smile:


You see something printed and it looks right to you, so you assume everything is right. That's the mistake you're making. Instead, you need to test for the described behaviour, in this case it's complaining about the return value of the function:

print 'the function returns: {!r}'.format(double_list([0, 1]))


I tried with return command, and i still don't manage to get it work.
Thanks for the effort.


Thanks for your help, but i don't understand it.
Can you maybe simplyify it.
Thanks for your effort in advance.


Effort and figuring out what you need to know next is on you :wink:
You can't shift either of those onto others


When describing the action of a program, always be specific about the related details.


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

print double_list(n)

So far i understand this, but i don't have any clue what is missing.
Can you maybe explain it to me in layman's terms,


It wants you to return the list that you manipulated, (the entire list), in your case, that would be returning the parameter.
The parameter is x.

here you return x[i], but maybe just return x?


See @kyleaw 's post regarding what to include in the return statement.


Thank you all for your kind support, i understand it now.