What does index of lst even mean?


#1

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.


FAQ: Code Challenge: Lists - Double Index
FAQ: Code Challenge: Lists - Double Index
#2

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]

Terminology to add to your lexicon…

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

#4

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.


#5

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?


#6

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.


#7

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.


#8
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.


#9

That straightened it out for me, thank you!


#10

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! :smiley:


#11

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.

Bare 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.


#12

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


#13
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.


#14

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)


#15

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.