Skip first entry in for loop in python?

前端 未结 13 2469
面向向阳花
面向向阳花 2020-12-02 05:15

In python, How do I do something like:

for car in cars:
   # Skip first and last, do work for rest
13条回答
  •  一向
    一向 (楼主)
    2020-12-02 06:11

    To skip the first element in Python you can simply write

    for car in cars[1:]:
        # Do What Ever you want
    

    or to skip the last elem

    for car in cars[:-1]:
        # Do What Ever you want
    

    You can use this concept for any sequence.

提交回复
热议问题