Advanced Python Code Challenges: Lists Challenge 4

Why i still got this message " Make sure to define what should happen if index is too big!" ?

Challange 4
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:

double_index([1, 2, 3, 4], 2)

My code is like this:

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

It says something about what the problem is. Have you tried reproducing it?

When i tried executing this command, it ran smoothly.

print(double_index([3, 8, -10, 12], 2))
>>>[3, 8, -20, 12]
print(double_index([3, 8, -10, 12], 1000))
>>>[3, 8, -10, 12]

Finally, i found the problem. The code should be like this:

if index < len(lst):

without “=”

thanks for the advice

2 Likes

In order to describe all possible cases it is usually the edge cases where things begin and end that you need to account for. If you get a list you might think it’s okay to look at a first value, but a list can be empty and therefore not have a first value etc.
It’s not always easy to identify all edge cases but you’ll soon get into habit of at least trying.

Randomly generated input can also sometimes be used to throw all kinds of things at your code, which can help in finding edge cases.

1 Like

I never thought I would contribute to any forum but I was actually thinking about your solution, and though it is perfectly correct, I thought to suggest another solution:
using the insert function, I first inserted the * 2 of index (-10), then removed it using del function (mind you that now the index moved from 2 in this example to 3, hence index+1.
#Write your function here
def double_index(lst, index):
if index < len(lst):
lst.insert(index, lst[index]*2)
del lst[index+1]
return lst
else:
return lst
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))

Hi there, welcome to the forums.

That’s all well and good… except that the task asks you to return a new list, not mutate the original one. :slight_smile:

# double_index as you have implemented it...
def double_index(lst, index):
  if index < len(lst):
    lst.insert(index, lst[index]*2)
    del lst[index+1]
    return lst
  else:
    return lst

original_list = [2,4,6,8,10]
new_list = double_index(original_list, 2)

print(new_list) # output: [2, 4, 12, 8, 10]
print("true" if new_list is original_list else "false") # output: "true"

# double_index as the OP arrived at it...
def double_index2(lst, index):
  if index < len(lst):
    return lst[:index] + [lst[index] * 2] + lst[index + 1:]
  else:
    return lst
    
original_list = [2,4,6,8,10]
new_list2 = double_index2(original_list, 2)

print(new_list2) # output: [2, 4, 12, 8, 10]
print("true" if new_list2 is original_list else "false") # output: "false"

As you can see, your approach mutates the original list - demonstrated by testing whether new_list is original_list - and so technically fails to meet the given specification. :slight_smile:

That aside, it is a novel way of approaching it. :slight_smile:

1 Like

aside from whether or not the original should be mutated,

lists only support adding/removing things at the end, if you do it anywhere else then all the following values have to be moved.
Rather than adding and removing a cell, you could overwrite the one that is already there, you’re not actually looking to resize anything.

a reasonable approach would be to copy the list and then update the requested cell

Right, i did not pay attention to this bit! Thank you for the clarification.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.