Solution Discussion for lists code challenge

The code I showing is the solutions from codecademy from that exercise. Just need some explanation and probably your code more simplifies better than previous.

I think this has been explained about ‘inclusive’ and ‘exclusive’
https://discuss.codecademy.com/t/why-and-how-are-selecting-python-list-indexes-inclusive-and-exclusive/431896

If that is the given solution in the exercise then we are in a rabbit hole. Granted, it is a solution, but certainly not one that we might expect given the difficulty this problem has presented just with respect to indices. It goes way over the top.

List elements are mutable just as variables are. We merely point to the element by its index number and mutate away. There is absolutely no call for reconstructing the list, as clever as it may be to do so. It’s a waste of processing power. What if we are given a million lists to mutate? The manner in which we perform this task will need to be greatly simplified to save on resources.

2 Likes

My solution is

def double_index(lst, index):
  new_list = lst
  count = len(new_list)
  if index >= count:
    return lst
  else:
    list_sum = new_list[index] +  new_list[index]
    new_list[index] = list_sum
    return new_list

Is there a more efficient way to do this

this:

new_list = lst

i wouldn’t recommend. A) this doesn’t make a copy of the list. B) its not needed

we can modify the list.

so it is not necessary to initialize the list?

Depends on what you mean by initialize the list, lst is a parameter which gets its value from argument at function call. You understand function parameters and arguments?

I understand parameters and arguments, I think the confusion is from when I tried to run it without doing new_list = lst the compiler gave me an error.

That code i would like to see that code then, because both variable (new_list and lst) reference the same list in memory, it shouldn’t matter.

I’m guessing @sforce85 removed the line new_list = list but not the references to new_list in the rest of the function.

oh, i think i understand the mistake.

new_list = [2, 4, 6, 8]

def double_index(lst, index):
  count = len(new_list)
  if index >= count:
    return lst
  else:
    list_sum = new_list[index] +  new_list[index]
    new_list[index] = list_sum
    return new_list

double_index(new_list, 2)

@sforce85, did you have something like this? I think i better need to understand where new_list came from in your code. Did you have more code then you posted on the forum?

I’ve clearly been awake too long and made it way harder than it needed to be.

#Write your function here
def double_index(lst, index):
  new_list = []
  if len(lst) <= index: 
    return lst
  else:
    for i in lst:
      if i == lst[index]:
        new_list.append(i*2)
      else:
        new_list.append(i)
  
    return new_list
      
  
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))

Could be a good challenge for yourself to improve upon your solution?

Here is my solution:

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

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

Saw someones that looked similar and thought this was a bit cleaner. Thoughts/comments?

if we want to update a single element in a list we can just do:

the_list[index] = 'new value'

you could use that here, so you don’t have to use list slicing

Dear all, I came up with:
#Write your function here
def double_index (lst, index):
if index > len(lst):
return lst
double = lst[index] *2
#replace the index in the original list
lst[index]= double
return lst
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))

and I got the result: [3, 8, -20, 12]

This code returned the correct answer:

#Write your function here
def double_index(lst, index):
  if index >= len(lst):
    return lst
  else:
    double_index = lst[index] * 2
    lst_slice1 = lst[0:index]
    lst_slice1.append(double_index)
    new_lst = lst_slice1 + lst[index+1:]
    return new_lst
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))```

Returned:
[3, 8, -20, 12]