Modifying each element in a list in a function


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

n = [3, 5, 7]

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

print double_list(n)


a function ends the moment a return keyword is reached

When a return keyword is reached in a loop, the function still ends, the loop simply breaks so the function can end

so your loop ends the function in its first run/iteration



issue resolved. i understood, so after performing the function on the first element of the list, it just stopped after the first element without continuing further
so ,
the issue resolved after i just changed the indentation of the return function
thank you.!


yes, by placing the return outside the loop, the whole loop can run, the loop and function don't end in the first iteration/run of the loop

Very good :slight_smile: