/  Technology   /  The Slice Notation in Python
The Slice Notation in Python

The Slice Notation in Python

 

Before learning about slicing, let’s once give a peek at indexing.

 

Indexing is a way to access an element in a sequence type like a list, string, tuple, etc. We use square brackets to access them. 

 

Let’s consider a list:

>>> alphabet = ['a', 'b', 'c', 'd', 'e', 'f']
>>>alphabet[0]
'b'
>>>alphabet[-2]
‘e’

 

Now that we have understood how to access elements, we’ll now understand what the term slicing means. In programming, slicing usually refers to obtaining a substring, sub-tuple, or sublist from a string, tuple, or list respectively.

 

Python offers several ways to implement slicing operations, not only the three ways above but with any iterable.

 

Note: An iterable is an object that can be iterated over or can be read or accessed using a for loop.

 

Syntax:

[start_at : stop_before: step]

 start_at is the index of the first item to be returned (included)

stop_before is the index of the element before which the iteration stops (not included)

step is the stride or the jump between any two items.

 

All three of the arguments are optional which means you can skip using any of them.

a [start_at: stop_before]   items start from start_at and end at stop_before

a [start_at:]   items start from start_at print  the rest of the array

a [: stop_at]   items start from the beginning and end at stop_before

a [:]      a copy of the whole sequence

 

Let’s see some examples:

>>list = [1, 3, 5, 7, 9]
 
>>list[1:4]  
[3, 5, 7]             #start at 0, stop before 4
 
>>list[2:]   
[5, 7, 9]             #start at 0, stop at end of list
 
>>list[:3]
[1, 3, 5]             #start at 0, stop before 3
 
>>list[1:4:2]
[3, 7]              #start at 1, stop before 4, every 2nd element
>>list[2::2] 
[5, 9]              #start at 2, stop at end of list, every 2nd element
 
>>list[:3:2]
[1, 5]              #start at 0, stop before 3, every 2nd element
 
>>list[::2]  
[1, 5, 9]             #start at 0, stop at end of list, every 2nd element

>>list[::] 
[1, 3, 5, 7, 9]    #start at 0, stop at end of list

 

Note: Indexing always returns characters whereas slicing always returns the respective sequence.

 

All three of the arguments also accept negative indices. For start_at and stop_before, a negative index means we start counting from the end of the list instead of counting from the start

>>list = [1, 3, 5, 7, 9]
 
>>list [1:-2]    
[3, 5]                  #start at 1, stop before 2nd to last)
 
>>list [-3: -1]   #
[5, 7]                  #start at 3rd to last, stop before last)

 

A negative step means that the list is sliced in reverse (from end to start). This also means that start_at should be greater than stop_before and that stop_before in the context of a reverse stride is more like stop_after if you are looking at the list non-reversed. For example:

>>list = [1, 3, 5, 7, 9]
 
>>list[::-1]    
[9, 7 , 5, 3, 1]   #reversed
 
>>list[4:1:-1]  
[9, 7, 5]             #reversed, start at 4, stop after 1
 
>>list[-1:1:-2]
[9, 5]                  #reversed, start at last, stop after 1, every 2nd

 

Leave a comment