CODE CHALLENGE: LISTS Double Index double_index(lst, index)

Hello, i’m feel confuse when pratice with this link above. I’m newbie for Python.
There two function, the first is my and last is solution of codecademy :

 def double_index(lst, index):
    if index > len(lst) :
        return lst
    else :
        lst[index] = lst[index] * 2
        return lst

#print(double_index([3, 8, -10, 12], 1))
def solution_double_index(lst, index):
  if index >= len(lst):
    return lst
    new_lst = lst[0:index]
    new_lst = new_lst + lst[index+1:]
    return new_lst

#print(double_index([3, 8, -10, 12], -5))

I’ve tested and no result different between 2 function. (i’m not return new list as excercies requires, but i’m think it’s not important.)
Excercise didn’t let me pass for define what should happen if index was too big. Tested with index = 15 and my function return lst origiral.

Please tell me that my function work correctly with any list input and will double value index in lst just like solution code ?
Tested with index = -5 and 2 function throw error Out of range.

Try passing in a 4 item list - like the example of [3, 8, -10, 12], and specify an index of 4… You’ll see why your code fails.

Thanks, i’ve solved by change line 2 of my function to :slight_smile:
if index >= len(lst) :

It solve the problem if index was too big :slight_smile:

1 Like

They also ask you to create a new list instead of modifying the original

1 Like


I faced the same situation of [codecoder46561]. I modified a little bit the code:

def double_index (lst, index):
if (len(lst)-1)>=index and index>=0:
return lst[0:index]+ [2*lst[index]] + lst[(index+1):]
return lst

#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], -5))

It works… !! :slight_smile:

1 Like