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

There are various ways to skip the first entry in Python loops. But, first, you will need to determine which example best meets 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 a for loop. For this example, slicing notation [1:] is added to the end of friends, denoting the start position in the list.

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

for i in friends[1:]:
   print(i)
  • Line [1] creates a list of friends and saves it friends.
  • Line [2] instantiates a for loop, which starts at position one (1) in the list elements (‘Sam’).
    • LIne [3] outputs friends to the terminal (one per loop).

Output

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.

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

for idx, item in enumerate(grades):
    if idx == 0:
        continue
    print(idx, item)
  • Line [1] creates a list of grades and saves it grades.
  • Line [2] instantiates a for loop referencing the location (idx) and value (item) for each element in the list. This uses the enumerate() function.
    • Line [3] checks the if statement for the value of idx.
      • If idx equals zero (0), line [4] executes (ignoring the print() statement) and navigates back to the top of the for loop.
    • Line [5] outputs the location (idx) and the value (item) to the terminal.

Output

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)
from itertools import islice
cars = ['Porsche', 'Honda', 'Toyota', 'Mazda']

for car in islice(cars, 1, None):
    print(car)
  • Line [1] imports the required library, itertools, and islice.
  • LIne [2] creates a list of cars and saves it cars.
  • Line [3] instantiates a for loop, which starts at position one (1) in the list elements (‘Honda’).
    • LIne [4] outputs cars to the terminal (one per loop).

Output

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!