# What does index of lst even mean?

Double Index

I literally have no idea what this prompt is asking me to do. I’ve read it over and over and even checked the solution, but it still makes no sense. I’m finding myself hung up with this sentence in particular " The function should double the value of the element at `index` of `lst`…"

What does index of lst even mean? They’re both arguments of a function right? What am I missing here? Please help!

Edit: I think I’ve figured it out, but the whole prompt was written in a very confusing manner. I’d still appreciate someone else’s explanation just to make sure I fully understand. Thank you.

75 Likes

Given a list, say, `lst = [3, 6, 10, 15, 21]`, we can access any member of the list by polling its index. To access the `10` above, we would use, `lst[2]`.

The index of an iterable is a subscript of the object. `lst` is the object; `[2]` is the subscript. `2` is the index.

``````print (double_index([3, 6, 10, 15, 21], 2))
# [3, 6, 20, 15, 21]
``````

The exercise wants us to validate the inputs (index value, anyway) so we need a conditional that only permits the doubling code to run if the given index is within valid range of the list.

``````if index < len(lst):
// run the code
return lst
``````

In either event we return the list, but it will only be changed (mutated) if the index we supply is within range.

``````print (double_index([3, 6, 10, 15, 21], 5))
# [3, 6, 10, 15, 21]
``````

to poll means to access.
to query means to match

When we wish to know the value referenced by a variable, we poll the data, and when we wish to match a search term we query the data.

``````sample = [2, 4, 6, 8, 7, 5, 3, 1, 2, 2, 3, 2, 3, 4, 5, 6, 7]
print (sample[9])          #  2  =>  poll the data
print (sample.count(2))    #  4  =>  query the data``````
21 Likes

Thank you so much for your thoughtful and well-written reply! I was still a bit confused as to why the return wasn’t indented to the same level as the code that was being run, but you really helped clear that up for me. I also think that at first I wasn’t understanding that “index of lst” meant lst. I don’t know if it was the way it was worded or that I just wasn’t completely grasping the terminology but it makes more sense now.

8 Likes

Can you explain why lst[index] = lst[index] * 2 is necessary for the code to function? The rest of this makes sense, but I tried solving the problem again in a way that I thought made sense only to get a single number as an output. If the index is greater than the length of the list I still get the original list returned so that part is good. Why do I need the lst[index] = part to return the full list with the modification?

This returns only `-20`, not the list, and does nothing to modify the list. The expected result is a mutated list when the index is valid.

Yes, I’m aware that it only returns -20. I’m just trying to figure out why and how lst[index] = lst[index] * 2 works.

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

is an assignment. The evaluation on the right side is first performed, by polling the current value of the list at that index and carrying out the arithmetic, which result is then assigned to the list at the same index, thereby replacing the current value with the new one.

11 Likes

That straightened it out for me, thank you!

3 Likes

I don’t think the site has taught up to this point that you can replace certain indexes in a list with a different value… That’s where I was getting confused because I could only get the -20 part but had NO idea how to actually get it into the list.

Thanks!

8 Likes

It is important to follow progress from the dashboard. The only conclusion I can draw is that people are jumping ahead to challenges that would naturally follow units covering the concepts.

The natural order is buggy according to several reports which I have also confirmed. This can lead a learner to believe they are infact where they should be, but without knowing, are not.

Bear in mind that this is a code challenge, not a lesson. We need to be informed before entering this module. Anybody having trouble with this simple problem has obviously missed the unit on Lists, and on Lists and Functions.

I get stuck in this problem also, maybe because my english is not so good…

**The function should double the value of the element at index of lst : lst[0] * 2 ?

If `index` is not a valid index, the function should return the original list. What does it mean a valid index ? index is not the position of the 1st element in the lst?

How to return the new lst with the value doubled? I tried append but it doesn’t work, it adds the new value at the end…of course

``````lst = [11, 23, 45, 66, 78, 99]
``````

Indices of this list can be found in a `range` of the same length…

``````range(len(lst))    //  [0, 1, 2, 3, 4, 5]
``````

Given the function,

``````def double_list(lst, index):
``````

`index` must be a value from that range list to be valid. If not one of those six values, the index will be out of range.

We can access elements by their index.

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

will double the value of the item at `index` position in `lst`.

Thank you very much for your help Roy:

I did that:

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

but it doesn’t work… something I missed…of ocurse it prints only the index and not the new_lst with the new index…damned

Do I have to create a variable new_lst?

It prints : 6 (index doubled)

No, but you do have to modify the list that is passed into the function. If the index is valid (that is, within range) then modify the element at that index, then return the list.

2 Likes

I think I’ve missed something. I’ve been going through the Computer Science path and I’ve done all the modules in that, but I feel like I’m missing something. Is there a path I should have done before this?

1 Like

If you need some brushing up on the basics, look for the publicly accessible Python 2 course and go through all the units. It should help fill some gaps.

Finally understand, what’s happening in this challenge.

Just an FYI, I can confirm that replacing certain indexes within a list had not been covered prior to this challenge. I have not skipped a single lesson, task, challenge or project within the Data Science path and the detailed overview confirms I have checked every box. If I missed it somewhere, I would really appreciate someone sharing the URL for where this was covered. This is the first time I have felt completely confused by a challenge. Also, if anyone has a video walk-through of it, I would love to see it. Thanks!

12 Likes

Index is discussed in detail starting with this lesson,

Selecting List Elements I

There was a lesson in Python discussing the mutability of lists, but finding that lesson is trickier than one at first thought. It’s not in the Data Science Python unit on Lists, so must be in another course, somewhere.

The author may have to revise this particular course to include that lesson.

3 Likes

Hi. I also had a really difficult time understanding what the prompt was asking and, being brand new to Python, I also find some of the explanations difficult to follow. For my own notes, I wrote a breakdown of the function and solution and thought I would share it here in case anyone else finds it helpful.

line 1: def double_index(albums, index):
line 2: if index <= len(albums):
line 3: albums[index] = albums[index] * 2
line 4: return albums

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

line 1: define the function & parameters. I found that the parameter of “1st” kept tripping me up in the way I was reading the instructions for the challenge so I replaced it with ‘albums’ and used the parameters. (albums, index) when defining the function.

line: 2 As we already know, “len” counts the number of items in the list. Let’s pretend that there are 17 albums in this list.
The reason the equation contains just less than (<) instead of less than OR equal to (<=) is because a list with a count of 17 items would only have an index that goes up to 16 (0 to 16).
So, an index of 17 or higher would not be considered valid because the range of indexes would be 0 - 16 for a list that contains 17 items.

line 3: this line says to double the value that appears at the index specified. For instance, if the value of index 3 (item 4) is 5, this says to double that value so index 3 will be updated to 10.

The print statement is specifying that in a list of the values provided [3, 8, -10, 12] to double the value of the number located at index 2 (3rd in the list). In this case the 3rd number is “-10”, so the function multiplies -10 * 2 so the value at index 2 is updated to 20.

line 1: def double_index(albums, index):
line 2: if index <= len(albums):
line 3: albums[index] = albums[index] * 2
line 4: return albums

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

Result: [3, 8, 20, 12]

15 Likes