Python – How to Skip the First Entry in a Loop?

There are various ways to skip the first entry in Python loops. You will need to determine which example best satisfies your coding needs.

Skip First Entry using Slicing

Python offers a unique feature, slicing.  This feature enables the coder to quickly access various data types, such as lists, tuples, and strings. This feature is zero-based, which means the elements start at 0. Slicing is an alternative to using a loop.

The slicing format is: [start:stop:step]. If these parameters are not stipulated, the defaults are set to [0: len(x): 1] or [:]. By setting a start position of 1, the first element in the list will not display. However, the remainder will.

cars = ['Porsche', 'Honda', 'Toyota', 'Mazda', 'Mitsubishi', 'Nissan']
result = cars[1:]
print(result)

Output:

['Honda', 'Toyota', 'Mazda', 'Mitsubishi', 'Nissan']

To learn about the Python slicing feature, feel free to check out the following Finxter video tutorial:

Skip First Entry using a For Loop

Another way to skip the first entry in a list is to use the for loop. For this example, slicing notation [1:] is added to the end of friends, denoting the start position in the list. When this code runs, the for statement starts at position 1 ('Sam'). The loop iterates and displays the elements from the start position of 1 to the end of the list.

friends = ['Alice', 'Sam', 'Steve', 'Anna']

for i in friends[1:]:
   print(i)

The output shows all elements except the first one in the list:

Sam
Steve
Anna

Skip First Entry using Continue

The continue statement is another way to skip the first entry in a list.

In the code below, idx in the for statement displays the current index in grades. The variable item refers to the current value in the grades list such as 68, 82. The enumerate(grade) method keeps track of how many iterations have occurred. This method is needed to determine the position of idx in comparison to the end of the list.

The if statement checks the value of idx. If the value of idx equals 0, continue is executed (ignoring the print() statement) and loops back to the top. The iteration continues until the remainder of the elements display.

grades = [53, 68, 82, 41, 92]

for idx, item in enumerate(grades):
    if idx == 0:
        continue
    print(idx, item)

The output skips the first grade and its index:

1 68
2 82
3 41
4 92

You can learn more about the Python enumerate() function in the following video tutorial:

Skip First Entry using Itertools

This example uses the islice() method from the itertools library to skip the first entry.  The format of this method is:

 islice(iterable, start, stop, step)

In the code below, the first parameter of islice() (an iterable) is cars. The second parameter (start position) is 1. The third parameter (stop) is None. If this parameter is empty, only the first element will display.

from itertools import islice
cars = ['Porsche', 'Honda', 'Toyota', 'Mazda']

for car in islice(cars, 1, None):
    print(car)

The output is:

Honda
Toyota
Mazda

Thanks for reading this article from Finxter Creator Kat—in case you want to improve your Python skills, consider joining our free email academy. We have cheat sheets too!