13. Modify 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

Don't forget to return your new list!

print double_list(n)

That's my code. Here's the error I get:

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

So, instead of [6, 10, 14], I get [6, 5, 7].


#2

push the return x line 4 spaces back
i.e

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

#3

This is not what the instructions ask me to do, but I just wanted to try alternate ways of doing it

def double_list(x):
    for i in x:
        i = i*2
    return x

I get the error that it fails for the test case [0,1].
However when I included a print statement to check what was going on

def double_list(x):
    for i in x:
        i = i*2
        print i
    return x  
print double_list(n)

I get the output

 6
10
14
[3, 5, 7]
None

Clearly the elements are getting multiplied but the list is not being modified. Please tell me why this is so?


#4

This may be because you need to put x[I] = x[I] * 2, not i = i * 2


#5

Thanks for the tip. In a later exercise we are told that the difference between the two looping methods is that one allows only access to the items while in the other we can modify them.
for i in x, lets you read the items, but like you said x[i] *= 2 is needed to modify the item.


#6

Same issue because the instruction says to use argument "x". The code worked but the checker returned an error. Changed from argument "x" to argument "n" and the code checker passed.


#7

Hello, I am getting the right results.... however with the same error as above.
Here is my code,
n = [3,5,7]

print ("printing list")

print (n)

print ("multiplying each element by 2")

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

print ("printing final list")

print (n)

print ("now with function")

def double_list(n):
for i in n:
print (i)
return n
print (double_list(n))

Here is the output
6
10
14
[6, 10, 14]
None

here is the error:
Oops, try again. double_list([0, 1]) returned [0, 1] instead of [0, 2]

Can you please explain why i get the error?


#8

hmm, incorrect indents, as well as i = i*2 instead of x[i] = x[i] * 2
working indents:
n = [3, 5, 7]

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

#9

here is the snapshot,

could you please explain why i can't use n and have to change it to x... i am confused...:frowning:
line 1 - creating a list
line 5 iterating thru the length of list
line 6 - we will take each number i the list and multiply it by 2
line 13 - a function with argument n
that will be passed in variable 'n' above... or it can't be same argument as variable name?


#10

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

"this is the answer"


#11

thank you for your help....


#12

You can take n as the argument of your function and it will still work. But what i understand the use of functions is to avoid repetition. For example if you have several lists and you want to multiply all with 2, will you rather create a function for each or just one that you cane call all the list.

I don't know if i have answered your question or not,

here is my code
n = [3, 5, 7]

Don't forget to return your new list!

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


#13

You can take n as the argument of your function and it will still work. But what i understand the use of functions is to avoid repetition. For example if you have several lists and you want to multiply all with 2, will you rather create a function for each or just one that you cane call all the list.

I don't know if i have answered your question or not,

here is my code


#14

I had the same problem and it worked with your solution.But why ?


#15

I have a question about "return x" at the end of the def double_list(x): function. The first time I got it wrong by typing... return x[i]: see below -

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)

The answer I got was 14 - I know now to return x at the end of the def double_list(x): function, but I am racking my brain trying to understand why return x[i] would give 14 and not [6, 10, 14].

Understanding this will really help me in grasping the underlying concept.


#16

Hi! Why is this code returns an error message?

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

It results this:
"[6, 10, 14]
None"

It seems I've solved the exercise: "Create a function called double_list that takes a single argument x (which will be a list) and multiplies each element by 2 and returns that list." The code exactly do that.
But I had an error msg: Oops, try again. double_list([0, 1]) returned None. Did you remember to return the result? It has printed out with my good solution.


#17

I have the exact same code and it does not work - :frowning:


#20

x = [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 x

print double_list(x)


#21

Hi,
The Problem is return statement shoud be intended with the function Definion and not with the for loop!
Hence, the problem was solved just by pushing back return statements 4 spaces back!`

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)

#22

Hey,

The problem seems to be in your code indentation and Python gives more importance to spacing of every set of line you write.
Put a TAB on every line of your code (below def double_list(x) ). Plus you don't have a return function.
Here's my code for reference. I hope your problem will get solved.


n = [3, 5, 7]

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

Don't forget to return your new list!

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

print double_list(n)