In python, How do I do something like:
for car in cars:
# Skip first and last, do work for rest
The best way to skip the first item(s) is:
from itertools import islice
for car in islice(cars, 1, None):
# do something
islice in this case is invoked with a start-point of 1, and an end point of None, signifying the end of the iterator.
To be able to skip items from the end of an iterable, you need to know its length (always possible for a list, but not necessarily for everything you can iterate on). for example, islice(cars, 1, len(cars)-1) will skip the first and last items in the cars list.