Loops Too Difficult

I am a complete newbie to coding and am starting with Python. Is it just me or are the exercises sometime a little too difficult. They do not test the material previously ran through and they throw a curve ball in almost each section.

I am particularly struggling with loops at the moment. For instance one of the challenges I have looked at the solution and it is displaying:

#Write your function here

def odd_indices(lst):

  new_lst = []

  for index in range(1, len(lst), 2):

    new_lst.append(lst[index])

  return new_lst

#Uncomment the line below when your function is done

print(odd_indices([4, 3, 7, 10, 11, -2]))

This is completely new material to me and don’t know what the ■■■■ it means! I have gone over the loop section 5 times and am considering throwing in the towel. Does anyone else share the same frustrations?

Hi there, welcome to the forums.

There is some expectation on you as the learner to experiment with the knowledge imparted during the lesson on your own time. One should not necessarily expect to simply complete the lesson and arrive at a point of complete and thorough understanding.

If you are struggling to understand loops, the fix for this is to write some code using them and observe how they work. Can you write a functional loop? Can you break that code and understand why it no longer works? Can you take a broken piece of code using a loop, find and understand the flaw, and fix it? These are skills you will need to work on, as with any learning experience, in your own time beyond the “classroom” of the learning environment.

It should not be completely new at all, if you have completed the material on loops.

The loop itself is not particularly complex:

for index in range(1, len(lst), 2):

Python’s for loops work by defining an iterator to loop over, and stepping through it.

The iterator in this case is range(1, len(lst), 2). The documentation for range() is available here.

range(1, len(lst), 2) is creating an iterable which starts at 1, ends at len(lst) and increments by 2 each time. For the example list given, [4, 3, 7, 10, 11, -2], len(lst) is 6, so our iterable looks like:

[1, 3, 5] *

The for loop then takes each of these values in turn, and plugs them into the code inside the loop.

# iterable is [1, 3, 5]
# 1st iteration, index = 1

new_lst.append(lst[1])

# 2nd iteration, index = 3
new_lst.append(lst[3])

# 3rd and final iteration, index = 5
new_lst.append(lst[5])

Hope that helps, but in any case if you’re struggling I would again recommend that you experiment with loops on your own. :slight_smile:


* The iterable is not a list, but an instance of the range type. For simplicity, however, I have expressed it here as a list.

5 Likes