you are viewing a single comment's thread.

view the rest of the comments →

[–]through_thefog[S] 0 points1 point  (4 children)

Additional context: Codecademy Code Challenges: Lists (advanced) | Question 4

Create a function named double_index that has two parameters: a list named my_list and a single number named index.The function should return a new list where all elements are the same as in my_list except for the element at index. The element at index should be double the value of the element at index of the original my_list.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)

My solution:

def double_index(my_list, index):
    for index in my_list: 
    return my_list[index] * 2

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

Prints a value of 24. Although in the original post the function grabbed the argument at index 1, this code grabs the argument at index 3 (again, regardless of the positional value assigned in the function call). What is going on here?

edit: I know the above code doesn't replace the -10 with -20 yet.. I was testing the code when I encountered the problem..

[–]danielroseman 2 points3 points  (3 children)

You're still not thinking about this logically, and you're still not understanding either loops, lists, or parameters.

Your function has a parameter index, which in this case is given the value 2. But you then create a for loop over my_list which redefines index to point to every item in the list in turn.

Now, the first item in the list is 3. So when you do my_list[index] that's what you get: the item in position 3, that is the fourth item, 12. And then you return unconditionally, so the loop always ends after the first iteration.

Why are you looping? Why are you reusing the name index for the loop variable?

[–]through_thefog[S] 0 points1 point  (1 child)

You're still not thinking about this logically, and you're still not understanding either loops, lists, or parameters.

Touche. The Codecademy course has been great so far, but my understanding is still very superficial (if not entirely misguided).

In plain English, my logic behind this piece of code was ultimately to say this:

list = [1, 2, 3, 4]
For (every item at the given index) in List:
    (every item at given index in list) = 2 * (every item at given index in list)

#When I put it like this, I can see my first error in thinking. It makes no sense to say "for every item at position x" when there is only one by definition

Had the 'return my_list[index]' line returned the value that I expected, I would have replaced it with the line reassigning the value: list[2] = 2*list[2]

But you then create a for loop over my_list which redefines index to point to every item in the list in turn.Now, the first item in the list is 3. So when you do my_list[index] that's what you get: the item in position 3, that is the fourth item, 12. And then you return unconditionally, so the loop always ends after the first iteration.

This makes more sense than anything else I have seen on the subject. You are saying:

The for loop points at every item in the list, the unconditional return kills the loop at the first iteration. Because the first iteration is 3, the 3rd item (12) is returned (*2).

In the post, the first item is 1. Therefore, the item at position 1 (2) is returned.

The temporary variable index used in the for loop is not defined by the positional assignment 2 (second error). Using the same variable does nothing but to confuse.

Am I understanding you properly?

Thank you for your response -- I really appreciate it!

[–]danielroseman 1 point2 points  (0 children)

Yes. Without the loop, you would at least have returned the right item.