Trouble with the for loop


I expected the function double_list(n) to return the list n with all of its values multiplied by two. It returned them as they were originally.

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


Above that code is also included this line:
n = [3, 5, 7]
but it still doesn't work.


This has no effect on the list.

Consider going over the list and caching the result in another, and return that.

Alternately, you could use range() and the index to modify the list element in place.


This unit is quite a ways into the track, and we are dealing with lists and functions so it is fair to review one important built-in list method, list.index().

Using this method it is possible to work with the original code and item lookup.

        x[x.index(item)] *= 2


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