A Guide to Python List Slicing

Slicing lists in Python is easy once you understand the syntax. We go through it all with examples here.
Andrew Wood  •   08 June 2022
Andrew Wood  •   Last Updated: 08 June 2022

Python lists can be manipulated to create sub-lists using list slice notation or the slice object. This tutorial will go over slicing and provide a few examples to illustrate the various concepts covered. 

List Slicing Syntax

A sub-list can be quickly and easily extracted from a list without the need to loop through the items in the list. There are two ways to slice a list in Python; using what we call slice notation [start:stop:step] or using the slice(start,stop,step) method. Both methods function in much the same way and are equivalent.

# list slice notation
sub_list = original_list[start:stop:step] 

# list slice object
sub_list = original_list[slice(start,stop,step)]

# The two notations are equivalent
  • start refers to the index at which to start the slicing. The item at the start index is included in the sub-list.
  • stop refers to the index at which to stop slicing. The item at the stop index is NOT included in the sub-list.
  • step refers to the number of indices to jump during slicing. This defaults to 1.
  • startstop and step are all optional arguments and can be omitted as we'll show in the examples to follow. The first item in a list is located at index 0 which you should bear in mind when creating sub-lists.
  • startstop and step can be positive or negative numbers. A negative value indicates that the count will start from the end of the list rather than the beginning.

Let's look at some examples now.

List Slicing Examples

We'll start by defining a list with 15 items, starting with item 1 at index 0 and ending with item 15 at index 14.

listexample = ['item '+ str(i+1) for i in range(15)]
print(listexample)

Output

['item 1', 'item 2', 'item 3', 'item 4', 'item 5', 'item 6', 
'item 7', 'item 8', 'item 9', 'item 10', 'item 11', 'item 12',
 'item 13', 'item 14', 'item 15']

Basic List Slicing

# extract the 5th item (Item 5)
>>> print(listexample[4]) #5th item at index 4
"item 5"

# extract items 3 to 10 using slice notation
>>> print(listexample[2:10])
['item 3', 'item 4', 'item 5', 'item 6', 'item 7', 'item 8', 'item 9', 'item 10']

# extract items 3 to 10 using slice object
>>> print(listexample[slice(2,10)])
['item 3', 'item 4', 'item 5', 'item 6', 'item 7', 'item 8', 'item 9', 'item 10']

# extract items 10 to the last entry using slice notation
>>> print(listexample[9:])
['item 10', 'item 11', 'item 12', 'item 13', 'item 14', 'item 15']

# extract the first 5 elements of the list
>>>  print(listexample[:5])
['item 1', 'item 2', 'item 3', 'item 4', 'item 5']

List Slicing in Reverse

Remember that the last item in a list can be accessed by the index -1 so there is no need to calculate the length of the list if you need to find the last item.

# extract the 2nd to last item from the list
>>> print(listexample[-2])
"item 14"

# extract item 15 to item 10 in reverse
>>> print(listexample[-1:-7:-1]) # slice notation
['item 15', 'item 14', 'item 13', 'item 12', 'item 11', 'item 10']

>>> print(listexample[slice(-1,-7,-1)]) # slice object
['item 15', 'item 14', 'item 13', 'item 12', 'item 11', 'item 10']

You will recall that stop refers to the index at which to stop slicing but is not included in the sub-list.

List Slicing with a Step

The step defaults to 1 but can be modified to step through the list at any rate.

# every 2nd entry between item 1 and 10
>>> print(listexample[:10:2])
['item 1', 'item 3', 'item 5', 'item 7', 'item 9']

# every second entry from item 1 to the end of the list
>>> print(listexample[::2])
['item 1', 'item 3', 'item 5', 'item 7', 'item 9', 'item 11', 'item 13', 'item 15']

"""
Notice how we leave the start, stop or step blank when we use the defaults.
Default step is always 1.
Default start is index 0
Default stop is last index.
"""

To step in reverse you must indicate a negative step.

# every second entry from item 14 to item 1 in reverse
>>> print(listexample[-2::-2])
['item 14', 'item 12', 'item 10', 'item 8', 'item 6', 'item 4', 'item 2']

"""
Since we are reverse stepping to the beginning of the list the stop argument can be left blank.
An equivalent notation is:
listexample[-2:0:-2]
"""

Summary of List Notation

# The full notation
original_list[start:stop:step]

# notation can be omitted if a default value is used
original_list[start:stop] # from start index to stop index -1
original_list[start:] # from start index to end of list
original_list[:stop] # from beginning of list to stop -1

"""
start, stop, step can also be negative values. 
If negative then the array is counted from the end not beginning (reversed).

If you are slicing lists programmatically (within a function or loop) 
then it may be easier to use the slice object rather than the slice : notation. 
""" 
# slice object
original_list[slice(start,stop,step)] # recommended in a function or loop
Share this
Comments
Canard Analytics Founder. Python development, data nerd, aerospace engineering and general aviation.
Profile picture of andreww
Share Article

Looking for a partner on your next project?

Contact Us