Start index for iterating Python list

Vincent Catalano picture Vincent Catalano · May 27, 2011 · Viewed 164.5k times · Source

What is the best way to set a start index when iterating a list in Python. For example, I have a list of the days of the week - Sunday, Monday, Tuesday, ... Saturday - but I want to iterate through the list starting at Monday. What is the best practice for doing this?

Answer

Björn Pollex picture Björn Pollex · May 27, 2011

You can use slicing:

for item in some_list[2:]:
    # do stuff

This will start at the third element and iterate to the end.