๐Ÿ“– Beginning Python Programming /
Module: List & Collection

Slicing

๐Ÿ“– Beginning Python Programming / List & Collection / Slicing

Introducing List Slicing

Given a list L, we can extract individual item by using L[index] where index begins with 0. For example, L[0] to get the first item, L[-1] to get the last item.

We can also extract a range of items by using slicing. The syntax is [ start : end : step ].

For example,

L[0:5]

# We can omit 0 for the starting index.
# For example, this gets the first 5 items.
L[:5]

# We can omit end to indicate until-the-end.
# For example, this gets the last 5 items.
L[-5:]