Stuck Modifying each element in a list in a function


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/3/2?curriculum_id=4f89dab3d788890003000096#

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


I tried a different approach than the default code in the editor. I don't understand this code isn't working? Can someone please explain what am I doing wrong?


n = [3, 5, 7]

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


print double_list(n)


#2

the way you construct your for loop means i contains read only values from the list, any changes made to them won't persist, and the list certainly won't be modified


#3

But it returns '2' when I use the following code:

n = [3, 5, 7]

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



print double_list(n)

#4

i might have lied a little bit, the last value of the list + modifications is retained by i after the loop has finished running, but only the last value. Given the variable doesn't just cease to exist.

but the values before are constantly overwritten because the loop needs to assign the next value in the list to i, so they are not retained

and it doesn't make modifications to the list


#5

Hey @stetim94 ! Thanks for the explanation!

So I figured out a way to make my version of the code work! I just wanted to try out a different way to make it work :slight_smile:

But it returned the list 3 times to the console :sweat:

n = [3, 5, 7]

def double_list(x):
    new_list = []
    for i in x:
        i *= 2
        new_list.append(i)
        x = list(new_list)
        print x
    return x


print double_list(n)

Thanks for the quick response!


#6

i think the exercise wanted you to use range() which gives you indexes to modify the list

we can do math while appending, its great:

new_list.append(i * 2)

saves a line of code. We can just return new_list after the loop, no need for x, that should save some more lines

you have a print statement in loop (3 outputs) a function call with print statement (1 output) which accounts for all outputs


#7

Sweet! Thank you for the help. I was trying to figure out a way to achieve the same without using the range()

And ill keep this in mind new_list.append(i * 2)


#8

Well, then you succeeded :slight_smile:

Something which hasn't been covered yet, is list comprehension, it allows you to generate a new list on a single line:

n = [3, 5, 7]

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


print double_list(n)

the i * 2 should look familiar, its what we append() to the list. and for i in x is the loop.


#9

Wow! Really, thanks for the insightful reply! I think I understand how that works :smiley:
More power to you mod!


#10

I thought more about your reply & I was wondering how does python add the , required in lists with the following code:

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

Would really appreciate a reply :smiley: @stetim94

Your code works but I can't understand how does it add the , :sweat: -
[6, 10, 14]


#11

well, python takes care of this for us. after appending i * 2, it will add a comma

i mean, when you use normal approach:

new_list.append(i)

you also don't add ,. Python takes care of this for you as well


#12

Thanks once again @stetim94!


#13

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