Advanced python code challenges : lists part 4 double index

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

I am not sure why my code is not correct which is following:
#Write your function here
def double_index(lst,index1):
if index1 <= len(lst) and index1 > 0:
lst[index1] = lst[index1] * 2
return lst
else:
return lst
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 10))

when i check answer on this it says i need to put a check for index is too big:
here is the solution code:
#this is codeacademy code
#Write your function here
def double_index(lst, index):

Checks to see if index is too big

if index >= len(lst):
return lst
else:
# Gets the original list up to index
new_lst = lst[0:index]

Adds double the value at index to the new list

new_lst.append(lst[index]*2)

Adds the rest of the original list

new_lst = new_lst + lst[index+1:]
return new_lst

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

if i run the both code it gives me same answer, please advise, also solution code doesnot work for negative numbers, mine does.

Still a beginner, trying to learn!

Hello @harrym2386783121, welcome to the forums! Consider your condition here:

What happens if the index is the same the length of the list? The doubling index code would still run. But wouldn’t that throw an index out of range error? Also, can’t a list index be 0? So the condition index > 0 would stop the doubling index code from running, even though it would be a valid index.


In future, can you please format code when you post it? To see how to format code, review ->this<- thread.

2 Likes

To answer your questions:

  1. i agree on the same length part, but solution code is not giving the list without doubling the index, also let me fix this with operators.
  2. Solution code give error when i put a negative number, mine doesnot.
    I know my code has issues Sir, i have started python 3 days ago.
    my question is about the solution code, please explain on that.

here is my amended code format code:

if index1 >= len(lst):
  return lst
 elif index1 < 0:
     return lst
 else:
  lst[index1] == lst[index1] * 2
  return lst

really appreciate the help!

I think that code would work much better now! Just watch out here:

lst[index1] == lst[index1] * 2
            ^^
            here there is a comparison operator

You should use the assignment operator (=).

That is true.


I believe the solution code is just a longer-winded way of doing it (while also being quite memory expensive-by the fact it creates a whole new list). However, there is some benefits (such as not modifying the input list).

3 Likes

Awesome! thank you for the suggestion!
you guys are really good, hope i’ll be like you guys too :slight_smile:

3 Likes

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