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 :
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.
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?
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].
#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))
i am struggling to even understand the instructions.
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.
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.
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?
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-
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).
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.
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.
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),