Python Code challenges, double index solution using replace()

Hello,
I’m working on the advanced python code challenge #4 ‘double index’.
I came up with this solution using replace().

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

This gets the correct result no matter what I give as input to the function. for example, if I enter
print(double_index([3, 8, -10, 12], 0))
then I get the result
[6, 8, -10, 12]
the number at index 0 is 3. Doubled to give result 6 in the new list.
But the lesson is telling me it’s wrong. Does anybody know why?
Thanks!

I believe the instructions state it should return a new list whereas you’re returning a string. You’ll need to adapt your code to meet that as a requirement.

1 Like