Fix Python – Skip first entry in for loop in python?

Question

Asked By – Rolando

In python, How do I do something like:

for car in cars:
   # Skip first and last, do work for rest

Now we will see solution for issue: Skip first entry in for loop in python?


Answer

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 (not for any iterable though).

This question is answered By – Abhijit

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0