hi i am now to the python and i am going through the code challanges on list topic here is the task…

4. Double Index

Create a function named double_index that has two parameters: a list named lst and a single number named index .
The function should return a new list where all elements are the same as in lst except for the element at index . The element at index should be double the value of the element at index of the original lst .
If index is not a valid index, the function should return the original list.
For example, the following code should return [1,2,6,4] because the element at index 2 has been doubled:```
double_index([1, 2, 3, 4], 2)

solutiuon
#Write your function here
def double_index(lst, index):
# Checks to see if index is too big
if index >= len(lst):
return lst
else:
# Gets the original list up to index
new_lst = lst[0:index]
# Adds double the value at index to the new list
new_lst.append(lst[index]*2)
# Adds the rest of the original list
new_lst = new_lst + lst[index+1:]
return new_lst
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))

my code/solution

#Write your function here
def double_index(lst, index):
if index > len(lst):
return lst
else:
lst[index] = lst[index]*2
return lst
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))

it was working in all cases and it is mutch simpler but it was not accepted are the any floas in my solution thanks for the help…

#Write your function here
def double_index(lst, index):
if index > len(lst):
return lst
else:
lst[index] = lst[index]*2
return lst
# this will trigger the error/edge case you missed
print(double_index([3, 8, -10, 12], 4))

when the length equals the index you want to use, you get an IndexErorr. Which why your solution doesn’t work

once you fixed that however, I much prefer your solution to provided solution. Would you be so kind to provide the exercise url? That would really help us and other learners

The function should return a new list
The requirement was to make a new list not alter the list given.
You modified the existing list which is not what the caller wants. They may want to keep that list as was and use this new list for something else.

but you are not modifying the given list, its in the function so you are not working with global
variable you are in the scope i thing so… so the result ist just result of function so if you will save the result in original list and owerwrite it then it will be changet but pleas if i am wrong let me know…

#Write your function here
def double_index(lst, index):
if index >= len(lst):
return lst
else:
lst[index] = lst[index]*2
return lst
x = [3, 8, -10, 12]
print(double_index(x, 2))
print(x)

lists (and dictionaries) are passed by reference

But on the other hand, I don’t see why we would need a copy of the list. That makes this exercise a whole lot more complicated without adding much (if any) value.