CODE CHALLENGE: LISTS Double Index double_index(lst, index)

https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-lists-cc/exercises/double-index?action=resume_content_item

Hello, i’m feel confuse when pratice with this link above. I’m newbie for Python.
There two function, the first is my and last is solution of codecademy :

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

#print(double_index([3, 8, -10, 12], 1))
def solution_double_index(lst, index):
  if index >= len(lst):
    return lst
  else:
    new_lst = lst[0:index]
    new_lst.append(lst[index]*2)
    new_lst = new_lst + lst[index+1:]
    return new_lst

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

I’ve tested and no result different between 2 function. (i’m not return new list as excercies requires, but i’m think it’s not important.)
Excercise didn’t let me pass for define what should happen if index was too big. Tested with index = 15 and my function return lst origiral.

Please tell me that my function work correctly with any list input and will double value index in lst just like solution code ?
Tested with index = -5 and 2 function throw error Out of range.
Thanks.

3 Likes

Try passing in a 4 item list - like the example of [3, 8, -10, 12], and specify an index of 4… You’ll see why your code fails.

1 Like

Thanks, i’ve solved by change line 2 of my function to :slight_smile:
if index >= len(lst) :

It solve the problem if index was too big :slight_smile:

2 Likes

They also ask you to create a new list instead of modifying the original

2 Likes

Hi!

I faced the same situation of [codecoder46561]. I modified a little bit the code:

def double_index (lst, index):
if (len(lst)-1)>=index and index>=0:
return lst[0:index]+ [2*lst[index]] + lst[(index+1):]
else:
return lst

#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], -5))

It works… !! :slight_smile:

4 Likes

Hi,

I’ve been struggling with these challenges; I’m wondering why my line index = index *2 does not work. In other words, why does it have to be lst[index] = lst[index] * 2?

def double_index(lst,index):

if index > len(lst):

return lst

else:

index = index * 2

return lst

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

cheers!

Welcome to the forums!

Think about what the index variable is.

If we take the example list:

[3, 8, -10, 12]

we know that there are 4 items in this list, and that they are zero-indexed - so the first item is an index 0, the next item at index 1, and so on.

Your function takes a parameter index, to tell the code inside the function which position of the list should be doubled.

What you’re doing is simply doubling the value provided in as that parameter, not actually touching the list at all. You need to use the index to access the appropriate item in the list, hence why we do lst[index].

Does that make sense?

Ah, that makes perfect sense, thanks!

1 Like

or you can update the element inside the list

#Write your function here
def double_index(lst,index):
  if index >= len(lst):
    return lst
  else:
    new_index = lst[index] * 2
    lst[index] = int(new_index)
    return lst

#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))

…except that the task specifically asks you to return a new list, not manipulate the original one. :slight_smile:

Reading the spec is important.

1 Like

i am struggling to even understand the instructions.

  1. what prompts on the instruction to check the length of “index” to begin with?
    i would never thought that I would check the size of the list by reading the instruction after defining the parameters in the function.
  2. from " double_index([1, 2, 3, 4], 2)", the interger 2 outside of sq bracket is not an element in a list.
    does it signifies the index #2, which is third element in the list?
    Thank you

You need to check the length of the list, and compare it to the value provided for index, because if the index provided is outside the bounds of the list you’ll get an error if you try to double it.

Example: If my list is [1, 2, 3, 5, 8] and I try and double the value at index = 10, I can’t do it because there aren’t 11 items in the list and so no index 10.

Yes, the function definition is double_index(lst, index), so the second parameter in the function call is the index of the item to be doubled.

Hope that helps.

1 Like

My initial solution was as follows:

def double_index(lst, index):
if index >= len(lst):
return lst
else:
new_lst = lst[0:index]
new_lst.append(lst[index]*2)
new_lst.append(lst[index+1:])
return new_lst

However the output added the second append as a separate list within the list:
[3, 8, -20, [12] ]
why did it do this instead of just adding the last element to the same list?

1 Like

Your list slicing with a range creates a new list (this includes a list with a single element or even an empty list). Try the following for a brief example-

lst = [5, 10, 15, 20]
print(lst[2:3])
print(lst[3:])
print(lst[4:])

Your simplest steps at this point without re-writing your code would be to either concatenate the two lists with lst1 + lst2 or consider the .extend method which is designed to append multiple elements to a list when a sequential data type is passed to it (like a list).

Ah I completely understand now. Thanks for the explanation!

I had the same question. Why does the second iteration of append, nest a list within a list? I believe it is because append only takes one argument, and you are trying to pass it a slice. A slice could be 1, or multiple elements depending on the length of the list.

Welcome to the forums!

You’re correct in that because you’re appending a list slice, a list containing the element is appended rather than the element itself. However, this isn’t because passing a list slice is invalid due to .append() taking only one argument. A list slice is a single object, so it would be a valid argument. Instead, this is because list slicing creates a copy of the original list (example below) and is, therefore, a list itself.

>>> lst = [0, 2, 4, 6, 8]
>>> slice = lst[1:3]
>>> type(slice)
<class 'list'>

To append the elements from index + 1 to the end of the list, use @tgrtim’s suggestion, as follows.

2 Likes

Great explanation. Thank you for clarifying!

1 Like

Hi, I was trying a similar approach, basically using concat to put the whole new list on one line. But I kept getting “type error, can only concat str not int” or somethng like that. My problem was I didn’t have all the and () in the right places. I am not sure why your way works. Is there something about and () that indicates INT or STR?

The error is saying that you have tried to add a string and an int together (possibly with the + operator)? Why not check the contents of the variables you’re trying to combine and ensure they contain what you expect them to (print or similar would help with this),