# Solution Discussion for lists code challenge

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

so I tried this and it worked. Important note here is that (beginner terms) lst[index] = lst[index]*2 this would mean that the element of the list at index will be multiplied by 2.
as per example:

print(double_index([3, 8, -10, 12], 2)):

our index here is 2 (as defined in the function).
lst[index] = lst[2] (in python terms 3rd position on the list) = -10

we multiply the number at index (i.e. -10 as it is on 2nd position) by 2
so lst[index]*2 = -10 * 2 = -20

Without using anything not learned in python to date, the longer version to make this work would be as follows (I know it’s a bit clunky, just wanted to make it super readable):

def double_index(lst, index):

# determine if it is before the last index

if index < len(lst) and index >= 0:
doubled = lst[index]
doubled = doubled * 2
new_list = lst[:index] + [doubled] + lst[(index+1):]
return new_list

# to account for -1 as last index indicator

elif index == -1:
doubled = (lst[-1] * 2)
new_list = lst[:-1] + [doubled]
return new_list

# if bad index is found

else:
return lst

I would just like to point out, that I created the successful result using, try and except(which was not counted as correct), as well as using if else(which also was not considered correct). I deleted it now, but in the solution given, entering negative numbers beyond -1 will return an error and doesn’t return the list, so it really doesn’t fully solve the problem. It kept telling me I need to double whatever was being pointed to in the index, but I was doing that and receiving the correct answers. Unfortunately I deleted my answer to see the solution, but might be something to look into.

It took me a while to get it, thanks

1 Like

won’t `new_lst.append(lst[index]*2` just add the doubled index to the list ? So the list would contain the orginal index and the doubled index?

Hi python_people!
Regarding this exercise. So far I understand the list, slicing, sort, length and etc. But for this exercise, I’m clueless and confusing. Then I just see the solution and starting to understand. Indeed got the question.

``````new_lst = lst[0:index]
new_lst.append(lst[index]*2)
new_lst = new_lst + lst[index+1:] # need explanation here?
return new_lst
``````

sorry for the spoiler code. Cheers ans many thanks!

Not a problem. It’s not really a spoiler since it is far more complex than the real solution needs to be. Does your code return,

``````[5, 10, 15, 40, 25]
``````

when given the list,

``````print (double_index([5, 10, 15, 20, 25], 3)
``````

?

``````def double_index(lst, index):
if index >= len(lst): # length function for the list less than index
return lst
else:
new_lst = lst[0:index] # slicing list to assign new variable
new_lst.append(lst[index]*2) # append function to update index multiply by 2
new_lst = new_lst + lst[index+1:] # here I'm lost what for index+1?
return new_lst
``````

It should return ([5, 10, 15, 40, 25])

why is the code so complicated? You have index, so you can just assign a new value to that index:

``````the_list[index] = 'new value'
``````

the new value then simply should be the current value at that index times two

using slicing just makes things more complicated

1 Like

The code I showing is the solutions from codecademy from that exercise. Just need some explanation and probably your code more simplifies better than previous.

I think this has been explained about ‘inclusive’ and ‘exclusive’

If that is the given solution in the exercise then we are in a rabbit hole. Granted, it is a solution, but certainly not one that we might expect given the difficulty this problem has presented just with respect to indices. It goes way over the top.

List elements are mutable just as variables are. We merely point to the element by its index number and mutate away. There is absolutely no call for reconstructing the list, as clever as it may be to do so. It’s a waste of processing power. What if we are given a million lists to mutate? The manner in which we perform this task will need to be greatly simplified to save on resources.

2 Likes

My solution is

``````def double_index(lst, index):
new_list = lst
count = len(new_list)
if index >= count:
return lst
else:
list_sum = new_list[index] +  new_list[index]
new_list[index] = list_sum
return new_list
``````

Is there a more efficient way to do this

this:

``````new_list = lst
``````

i wouldn’t recommend. A) this doesn’t make a copy of the list. B) its not needed

we can modify the list.

so it is not necessary to initialize the list?

Depends on what you mean by initialize the list, `lst` is a parameter which gets its value from argument at function call. You understand function parameters and arguments?

I understand parameters and arguments, I think the confusion is from when I tried to run it without doing `new_list = lst` the compiler gave me an error.

That code i would like to see that code then, because both variable (`new_list` and `lst`) reference the same list in memory, it shouldn’t matter.

I’m guessing @codingsheba removed the line new_list = list but not the references to new_list in the rest of the function.