# Question 4, advanced Python list challenges help

Ok, so, I’m utterly confused by question 4 of the python lists challenges here: Python Lists | Codecademy

So this is the challenge here and honestly I’ve read it over 6 or 7 times, even looked over the answer a number of times but I’m so lost. I’m starting to think its just written badly but if somebody could clear this up for me that would be amazing. I think I’m struggling with the difference between Value and Element in particular.

1. Double Index
Our next function will double a value at a given position. We will provide a list and an index to double. This will create a new list by replacing the value at the index provided with double the original value. If the index is invalid then we should return the original list. Here is what we need to do:

Define the function to accept two parameters, one for the list and another for the index of the value we are going to double
Test if the index is invalid. If its invalid then return the original list
If the list is valid then get all values up to the index and store it as a new list
Append the value at the index times 2 to the new list
Add the rest of the list from the index onto the new list
Return the new list
Code Challenge
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)
After writing your function, un-comment the call to the function that we’ve provided for you to test your results.

Hint
We can use slicing to get the values up to an index lst[0:index] and from an index to the end lst[index+1:]. Also, to append to the end of a list we can use the append() function.

``````#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))
``````

The code makes sense to me but the instructions are really confusing, is that just me? Are there basic concepts that I’m lacking understanding in?

Thanks.

Writing code based of instructions (requirements) and coming up with a solution is the challenging part. We call this problem solving, that is a challenging and a skill which needs training/practice.

actually, I find the code confusing. The whole list slicing is totally redundant. You can simple do:

``````def double_index(lst, index):
# Checks to see if index is too big
if index >= len(lst):
return lst
else:
lst[index] *= 2
return lst
print(double_index([3, 8, -10, 12], 2))
``````

Sure I understand that its a problem to be solved, it just wasn’t clear to me what the instructions wanted me to do. I can’t understand the wording used in the problem. Thanks for the more simple code, I don’t understand this part though:
else:
lst[index] *= 2

what does *= 2 do? I understand * on its own is multiplication and ** is to the power of, but I’ve not seen *= as of yet.

Cheers!

that is the challenging part, people might use wording/phrasing which might confusing. In a real world scenario (where you deal with a client who doesn’t even know what they want), its even worse. So its a skill to be able to dissect and extract the useful information

translating instructions into code is something people underestimate. It will get better with practice

yea, you can also write:

``````     lst[index] = lst[index] * 2
``````

but I was lazy, and wrote the short-hand (similar to `+=` and `-=`).

1 Like

Great, I appreciate the help!

I disagree. This solution leaves out discussion of pointers and how creating a new list and copying it over (new_list = lst) will just point at memory location of the original list. Thus multiplying whatever index by 2 will change both new and original list. It’s way over this part of the course students’ heads and their explanation of needing to splice the list doesn’t address the reason this is being done.

Even if you do not want to modify the original list, I would still avoid using slicing. There are other ways to copy the list:

``````new_list = lst.copy()
``````

The slicing is just confusing and obscures the readability

But yes, the course seem to avoid talking about pointers

My CS101 teacher forbid us from using methods so I was unfamiliar with this copy one.

I was just trying to explain that using lst[index] = lst[index]*2 is not the same… as previous post had stated. It does get the correct answer though. as far as passing the challenge. I agree that the splicing is strange way of doing this. I would have probably iterated lst to create new_list.
``````# copy list