List index out of range error being thrown


#1

Hello,

The output values are accurate, the array n=[6,10,14] returned from function

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

Don't forget to return your new list!

return x`

with n = [3,5,7] as an input is certainly an accurate result. What is causing for i to be out of range?

I have read answers to similar errors on the forum and I have not found the solution


#2

@2966841,

#
# using the list append() method
#
def double_list(x):
    #x=0
    new_list = []
    for i in x:
        new_list.append(i * 2);
    #end of FOR-loop
    #Don't forget to return your new list!
    return new_list

n = [3,5,7] 
print "{0} {1}".format("new_list : ",double_list(n))


#
# using the list insert() method
#
def double_list(x):
    #x=0
    new_list = []
    print "k v R"
    for key,value in enumerate(x):
        print "{0} {1} {2}".format(key,value,value*2)
        new_list.insert(key,value * 2)
    #end of FOR-loop
    #Don't forget to return your new list!
    return new_list

n = [3,5,7] 
print "{0} {1}".format("new_list : ",double_list(n))


#
# using the list square-bracket-[ ]  method
#
def double_list(x):
    #x=0
    new_list = range(len(x))
    print "{0} {1}".format("new_list created with range(len(x)) =>",new_list)
    print "k v R"
    for key,value in enumerate(x):
        print "{0} {1} {2}".format(key,value,value*2)
        new_list[key] = value * 2
    #end of FOR-loop
    #Don't forget to return your new list!
    return new_list

n = [3,5,7] 
print "{0} {1}".format("new_list : ",double_list(n))

# google search in stackoverflow.com
# python adding element to list by index site:stackoverflow.com
# python creating  list with range element  site:stackoverflow.com