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))

Hi, I (think I) understand what you mean, that it should be index>=len(lst), but I don’t understand why. The list has 4 numbers, so if the index is 3, wouldn’t it just multiply the last number in the list (since it starts at zero) by 3 and that be the end of it? And if the index were 4, why would that not be covered by index>len(lst)?

Does that make sense? I don’t dispute what you’re saying, I just don’t get why it’s true?

Edited for clarity.

Hi @java8100890366,

Following is the code that was under consideration, with some statements added for testing it:

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

numbers = [1, 1, 2, 3, 5, 8, 13, 21, 34]
length = len(numbers) # length is 9

print(double_index(numbers, length + 1)) # index exceeds length
print(double_index(numbers, length - 1)) # index less than length
print(double_index(numbers, length)) # index equals length

This is the result:

[1, 1, 2, 3, 5, 8, 13, 21, 34]
[1, 1, 2, 3, 5, 8, 13, 21, 68]
Traceback (most recent call last):
  File "/Users/glennrichard/Documents/double_index.py", line 13, in <module>
    print(double_index(numbers, length)) # index equals length
  File "/Users/glennrichard/Documents/double_index.py", line 5, in double_index
    lst[index]*=2
IndexError: list index out of range

Due to the if condition, the code executed without trouble when index exceeded length, with lst returned as is. With that situation, we avoided an IndexError.

Due to the elif condition, the code also ran without incident when index was less than length. The value of the appropriate item was doubled.

However, when index was the same as length, the if condition allowed execution of this line to be attempted:

    lst[index]*=2

An IndexError occurred because len(numbers) is 9, and that is the value of index, which exceeds the position of the final item. We needed this header for the if block:

  if index >= len(lst):
2 Likes

OHHH I get it now. Thanks for the reply.

1 Like