Https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-lists-cc/exercises/double-index

def double_index(lst, index):
if index >= len(lst):
return lst
else:
before_index_element = lst[:index]
double_of_element = [2*lst[index]]
after_the_element = lst[index+1:]
new_lst = str(before_index_element + double_of_element + after_the_element)
return (new_lst)

Hi, my answer is coming out right but still it’s showing " Double the value at position index in lst" error and has wronged the code(showing a cross).

What do you think of this?

Same problem as in this topic? I know its a different exercise, but the problem appears the same

Yeah, the code works fine. Gives the expected answer but showing the error below the writing space. On the right side, no error shown.

Here also, removing str did the work. :slight_smile:

1 Like

Why does the before_index_element = lst[:index] not require braces on the right side of the = but the following lines do?

Doing some testing with the exercise because it’s taking me longer to grasp.

ef double_index(lst, index):

if index >= len(lst):

return lst

else:

new_lst = lst[:index]

double_index = lst[index] * 2

new_lst.append(double_index)

after_index = lst[index + 1:]

new_lst.append(after_index)

return new_lst

why does this produce braces around the last item and not the first 3?

Just note that the original post here is from February and was solved at the time.

Brackets used like this are used to create list literals, a = 3 assigns integer 3 to the name a but a = [3] assigns a list (which itself contains integer 3) to this name instead.

As for why there are extra brackets in your output at present it’s because you have nested list objects (an element of your list is itself a list).

Bear in mind that your list slice always returns a list (even if it’s an empty one). If you then .append that list to another list it does just that. You wind up with an element of your first list that is itself a list.

A quick example-

a = [3]  # a list
b = [1, 2, 3]
b.append(a)
print(b)
Out: [1, 2, 3, [3]]  # we've put that list object inside our list

Perhaps reconsider your code (what about concatenation?) and its order so that you wind up with a single list of integer values.