Yours looks a lot better than mine. I like that // and I’ll have to remember that throughout the course. It looks like it gives an integer answer instead of a float. That would’ve been good to know so I could avoid trying to turn my numbers into integers. I’m gonna play around with insert() when I get the chance. That looks a lot easier to use too. Thanks for the help.

Is this a problem that should need an insert or append method?

- test if index is in range
- if in range then access the element at that index
- multiply it by 2
- assign the new value to the element at that index

WARNING ** answer below incase you still trying:

The instructions for this section was not very clear. I tried several times to get this but had no luck over 2 days. I think they did not provide very good information to conduct this challenge. I asked for the solution after trying several times.

I’m going to share the answer they provide to talk about and also the instructions.

Here is instructions and hit:

Create a function named `double_index`

that has two parameters named `lst`

and `index`

.

The function should double the value of the element at `index`

of `lst`

and return the new list with the doubled value.

If `index`

is not a valid index, the function should return the original list.

After writing your function, un-comment the call to the function that we’ve provided for you to test your results.

**Hint:**

Use `len(list)`

and `index`

in an if statement to check to see if `index`

is a valid index.

Correct answer provide by them:

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

What do you guys think?

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

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

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’

https://discuss.codecademy.com/t/why-and-how-are-selecting-python-list-indexes-inclusive-and-exclusive/431896

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.

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?