What does this code do exactly?

im at the loops excercise for python.
i cant figure out what this code do.
can someone help me?
this is the code what i cant understand by myself:

Hello! This code just gets every second element of the lst list, and appends it to new_lst:

def odd_indices(lst): #defines the function
  new_lst = [] #creates a new list-and assigns it to new_lst
  for index in range(1, len(lst), 2): #for every index in the range
#of numbers from 0 to the length of lst, skipping to every second element
#remember the order for the range function goes "start, stop, skip".
    new_lst.append(lst[index]) #appends the index described above to
#the new_lst
  return new_lst #after all iterations are complete, this returns the new_list

I hope this helps!

Is that the given solution to the exercise in Codecademy, or did you find the solution somewhere else?

If you’re struggling at that point in the course, don’t be afraid to spend some extra time on the material. Racing ahead without proper understanding doesn’t help anyone. :slight_smile:


it was the solution given me by code academy.
im not the one who is rushing trough the lessons. its the 5th time im repeating this. i could understand the code by myself what it excactly did. the answer above you gives me a good explenation.
im not skipping a lesson until i do understand what the code does. after im done and understand the lessons im doing the practices on my phone on daily basis!


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