Advanced Python Code Challenges: Lists

Create a function named double_index that has two parameters: a list named lst and a single number named index .

The function should return a new list where all elements are the same as in lst except for the element at index . The element at index should be double the value of the element at index of the original lst .

If index is not a valid index, the function should return the original list.

For example, the following code should return [1,2,6,4] because the element at index 2 has been doubled:
Create a function named double_index that has two parameters: a list named lst and a single number named index .

The function should return a new list where all elements are the same as in lst except for the element at index . The element at index should be double the value of the element at index of the original lst .

If index is not a valid index, the function should return the original list.

For example, the following code should return [1,2,6,4] because the element at index 2 has been doubled:

Need Help Understanding

This is something to check for at the top. The suggested solution will likely have this line,

if index < len(lst):
    # do this
return lst

Or it may be something like,

 if index >= len(lst): return lst
 # do this
 return lst

Once we confirm that the given index is valid then we can access that element in the list, double its value and then reassign it to the same index.

2 Likes

You could also consider a condition such as the following, in order to include valid negative indexes:

  if -len(lst) <= index < len(lst):
2 Likes

I also have difficulties with this exercise. My code:

 def double_index(lst,index):
   if index>len(lst):
     return lst
   elif index<=len(lst):
     lst[index]*=2
     return lst
 
 #Uncomment the line below when your function is done
 print(double_index([3, 8, -10, 12], 2))

When I run it, it runs fine. But I can’t pass the “check Answer” check. What did I do wrong?

Hello, @quills, and welcome to the Codecademy Forums!

Your posted code is not formatted, therefore it is difficult to read.

See How to ask good questions (and get good answers) for information on how to format code for posting.

You have:

if index>len(lst):

What happens when index is equal to len(lst)?

Thanks, changed the formatting.

Ok, I see where the question is going. I rearranged the equal sign and it works.

def double_index(lst,index):
  if index>=len(lst):
    return lst
  elif index<len(lst):
    lst[index]*=2
    return lst
 
 #Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))

Can I interpret it like this, if index == len(lst) the index counts the snippet after the last list item, which is nothing.

1 Like

Consider a four item list, like your example:

[3, 8, -10, 12]

List’s are indexed from 0, as I’m sure you know, so the index of the last item 12 is 3.

len([3, 8, -10, 12]) will return a value of 4, because there are four items in the list.

If we tried to access an item at index 4 from that list, i.e. index = len([3, 8, -10, 12]), we are trying to access an index which doesn’t exist. This causes an IndexError exception, because it simply doesn’t exist.

2 Likes

#Write your function here
def double_index(lst,index):
if index >= len(lst) or index < 0:
return lst
else:
double_index_new = lst[index]*2
new_lst = lst[:index] + [double_index_new] + lst[index+1:]
return new_lst

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