# Code Challenge: Lists - Frustration!

#1

Ok, so here is the deal: I am struggling greatly with the code challenge for lists. I have gone back and redone the entire material for lists three times, and I still do not completely understand the first challenge for lists. I don’t believe that the material that has been taught gives us sufficient knowledge to answer the code challenge successfully. I have posted previously on the first challenge, which is titled double index. Am I missing something? Was the information for this question (and the next one, by the way) taught in the lists training? If so, can you show me exactly where so I can learn the material properly? I am at total confusion and am feeling lost and hopeless. I will paste first problem below.

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.

#2

Stepping back for the moment, please describe what you know of a Python `list` object? How many exercises have you completed that involved lists?

#3

I have completed all the assignments up to this point over lists. They talked about setting range, length of a list, slicing, adding lists, zip,
And more. I don’t remember them ever talking about what is required in the question. I don’t remember talking about if an index is valid. Thanks for your reply!

#4

Hi @demok83,

Please provide a link to the exercise that asks us to create a `double_index` function, so that we can better assist you with it.

#5

I could have sworn that I included it in the original post. Oh well. Here is a copied version of the supplied answer from codecademy on the problem.

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

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

Thanks guys!

#6

This is a link to the current exercise that I am working on, plus the code beow.

#7

Thanks for the link and the code.

In addition to what @mtf has asked regarding what work you have done thus far, let’s consider your current interpretation of what we are being asked to do here.

What do you anticipate should be displayed by the following statement after you have correctly written the `double_index` function, and why? …

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

Now, let’s revise the above statement. What should be displayed by this one, and why?

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

#8

Actually i second this, lists instructions must be more in-depth, as i remember, i struggled with it aswell.
I was doing Computer Science Path too, just to get into programming, to understand those algorythm things etc etc etc, because i was completely new. But i am planning to became web dev, so i switched to Web development path, once i felt power
So, comparing Javascript’s arrays section to Python lists section, i must say, i like how they handled instructions in Javascript, they always give you links to documentation, it’s explained that arrays are mutable, that some methods change the array and others not. I think more focus on that needed in lists sections of Python.

#9

The code that was put in there is the code that codecademy gave me after I missed it a few times. It is not my own work. However, I have tried to analyze the answer to figure out why it is the way it is. There are a few things that I do not understand.

The first thing that is confusing is why do I have to know the length of 1st? How does it correlate to the input index Also, I don’t recall anything in the lessons that tell us how to determine if an index is valid. Not understanding this is much of the issue of the question itself.

#10

**I would expect it to give back some error since there isn’t an index of 9. **

I just don’t understand what the original question is asking and what purpose this does in reference to what we have already learned.

#11

The length of the list can be used to find out the index of the last item in the list. That last index is one less than the length of the list. A valid index is an index that exists in the list, so a number that is higher than the last index is not a valid index.

For a valid index, you are being asked to write code that finds the value at that index and to replace it with a value that is twice the original value. Then return the list. If the index is not valid, then just return the original list as it was received during the function call.

#12

Thank you so much. You have been SUPER helpful! I think I finally get it. Please look out for more of my posts because they are sure to be coming!

#13

You still never answered my question, or showed us your own work to help us spot where your difficulties may arise. You help us, and we help you. Just knowing the answer is seldom enough to re-inforce weaknesses.

#14

@mtf

I have completed all the assignments up to this point over lists. They talked about setting range, length of a list, slicing, adding lists, zip,
And more. I don’t remember them ever talking about what is required in the question. I don’t remember talking about if an index is valid. Thanks for your reply!

I directed this answer towards you earlier in the post. I now understand what was being asked of me. I was confused about the relationship of the validity of the index and the length of the list. Once I realized this, it became much easier. Thank you for you help, I truly appreciate it.

#15

Say we have a list of five items…

``````pets = ['dog', 'cat', 'goldfish', 'canary', 'gerbil']
``````

We see that the length is 5,

``````print len(pets)    =>  5
``````

From that we can assess an index as valid or not valid depending whether it fits the range,

``````[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]

print pets[-5]    =>  dog

print pets[-1]    =>  gerbil

print pets[4]     =>  gerbil

print pets[5]     =>  IndexError: list index out of range
``````

The solution given,

``````if index < len(lst)
``````

is sufficient at keeping us from going out of range in the positive direction, but does not prevent us going out of range in the negative direction.

``````-10 < len(lst)    # True
``````

For this, Python gives us the `range()` tool.

``````if index in range(-len(pets), len(pets))
``````

The range it compares our index to will be the one illustrated earlier, above.