← All lessons

List slices

Sign in · Sign up

Lesson 5.2

A slice takes part of a list, just like with a string: the right bound is not included.

games = ["Minecraft", "Roblox", "Brawl Stars", "Among Us"]

print(games[:2])    # the first two
print(games[-2:])   # the last two
print(games[1:3])   # from 1 to 2 inclusive

The result of a slice is a new list, not a piece of the old one.

The list items and the bounds start and stop are set by the check. Print, in order:

  1. the first stop items;
  2. the last stop items;
  3. from start to stop inclusive.
Hint

A piece of a list is set in brackets with a colon: [start:end] — for example, items[:stop] and items[start:stop + 1].

You need three slices of the same variable: from the start to stop, from -stop to the end, and from start to stop inclusive — the last one needs stop + 1.