• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

For Loop Question

In the following code, I'm trying to understand why N is not the first number in the list for each iteration, but instead is 1,3,5,7,9.

#Create a list of numbers
List  =  [1,2,3,4,5,6,7,8,9]

#Cycle through the list,
#each time removing the first number and printing the rest
for N in List:
    del List[0]
    print N, List

Open in new window

1 Solution
Because you are removing it but the index still advances 1
So at 1, you print it and remove it (index is at 0)
Next iteration you now have 2,3,4 etc but the index has advanced to 1 which is now number 3.
Try something like

while List:
	print List

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now