# Modifying each element in a list in a function

#1

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)

(Question)
If I insert return double_list(x) instead of return x above, I get an error message below.
What is wrong with it? Why should it be return x ?
:

Traceback (most recent call last):
File "python", line 9, in
File "python", line 6, in double_list
File "python", line 6, in double_list
File "python", line 6, in double_list
( and these lines go on endlessly)

#2

because `x` contains the list you modified, so this is what should be returned

if you put `return double_list(x)` there instead, you get recursion (the function calling itself), and given there is no base case, the recursion is endless (sort of like an infinity loop)

#3

Thanks a lot. I think I understand your explanation! Hopefully the more codes I write, the better it sinks in…!

#4

yep, it will

maybe this will help as well:

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

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

returned_result = double_list(n)
print returned_result
``````

`return` literally means that, it returns (hands you something back). Which we can capture in a variable, as you can see when we run this code

#5

Thank you very much for your help

#6