Start index for iterating Python list

前端 未结 8 1923
走了就别回头了
走了就别回头了 2020-12-23 02:52

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

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-23 03:29

    Why are people using list slicing (slow because it copies to a new list), importing a library function, or trying to rotate an array for this?

    Use a normal for-loop with range(start, stop, step) (where start and step are optional arguments).

    For example, looping through an array starting at index 1:

    for i in range(1, len(arr)):
        print(arr[i])
    

提交回复
热议问题