Odd Indices clarification - parameter is used in the .append method

I originally wrote the code below which is wrong:

def odd_indices(lst):
  new_list = []
  for values in range(1, len(lst), 2):
      new_list.append(values)
  return new_list

In the .append I forgot to add lst but I don’t understand why the parameter needs to be added back into the .append method? Why does this change the output so dramatically? How does passing the parameter back inside the function not turn it into chaos?

The correct code reads:

def odd_indices(lst):
  new_list = []
  for values in range(1, len(lst), 2):
      new_list.append(lst[values])
  return new_list

This is from Exercise 4 Odd indices here

If I’m not mistaken you’re looking to return the values of a list at the odd indices, so for a list of ["red", "orange" "yellow", "blue"] the return should be `[“orange”, “blue”].

Consider what values range(1, len(lst), 2) actually supplies. What relation do they have to your original list if not used as indices?

If you’re unsure at any point a little debugging with print or similar can help a great deal.

1 Like

Yeah I think I get it now, lst is being called in the .append method because we are pulling out the odd values from the parameter. Ipso facto it needs to be included.

Thanks.