Slice Notation on List in Python

Hello, Coders!! In this Python tutorial, we will learn about the slice notation on a list in a Python program.

Before diving into the main topic, let’s discuss some basic concepts first;

  1. List in Python
  2. Slice notation in Python 

List in Python

In Python, a List is considered as a data structure that is mutable or changeable in nature. It is an ordered sequence of elements.

The elements or the data values of a list are also called as items. It is defined by the elements placed inside square brackets []. It can include multiple number of items of different data types(int, float, char, etc).

Syntax:

list_name[item1, item2, item3,....]

Example:

bookstore = ['pen', 'book', 'eraser', 'marker', 'notebook']

Output:

['pen','book','eraser','marker','notebook']

 

Slice Notation In Python

The slice nation is used to return a portion of any list, string, tuple, etc. In other words, we can say it is used to obtain a sublist, sub-tuple, substring from a list, tuple, and substring respectively.

Syntax:

a[start:stop:step]

It is the common syntax for the slice notation, where the start is the index of the first element(included), stop is the index before which to stop (not included) and step is the stride between any two elements.

 

We can also use the slice notation by omitting the argument according to our requirement:

a[start:]  //Start from the given index till the last of the array
a[1:]  ⇾  starts at index 1 and stop the end of the array
a[:stop]   // Start from the first index to stop - 1 index of the array
a[:4]   ⇾  starts at index 0 and stops before index 4

a[start:stop]   // Start from the given index to stop - 1 index of the array
a[1:4]   ⇾  starts at index 1 and stops before index 4

a[:]       // A copy of the whole array
a[:]    ⇾  starts at index 0 and stop at the end of the array

a[start::step] // Start from the given index to last index of the array with a stride
a[2::2] ⇾ starts at index 2 and stops at the end of the array,every 2nd element

 

The start, stop and step maybe a negative number. In such a case, it counts the items from the last index of the  array instead of from the start.

Example:

a[-1]   ⇾ Last item of the array
a[-3:]  ⇾ Last 3 item in the array
a[: -1] ⇾ Every element except the last element
a[::-1] ⇾ All elements in a reversed order

Slicing a List in Python

Example1:

We can slice a list using the parameters discussed before. Let’s take a list of strings and then try to slice that list without any stride or step parameter.

season_india = ['autumn','Summer','Winter','Spring','fall','monsoon']
season_global = season_india[1:5]
print(season_global)

Output:

['Summer', 'Winter', 'Spring', 'fall']

 

Example2:

Slicing the list with a stride parameter:

season_india = ['autumn','Summer','Winter','Spring','fall','monsoon'] 
season_global = season_india[:5:2] 
print(season_global)

Output:

['autumn', 'Winter', 'fall']

 

Example3:

Slicing the list from the first index to a given index and from a given index to the last index of the array.

season_india = ['autumn','Summer','Winter','Spring','fall','monsoon'] 
season_global1 = season_india[:5]
season_global2 = season_india[1:]
print(season_global1)
print(season_global2)

Output:

['autumn', 'Summer', 'Winter', 'Spring', 'fall']
['Summer', 'Winter', 'Spring', 'fall', 'monsoon']

 

Example4:

Slicing the list to print the last 3 elements and to print every element except the last 3 elements.

season_india = ['autumn','Summer','Winter','Spring','fall','monsoon'] 
season_global1 = season_india[:-3] //excluding the last 3 elements
season_global2 = season_india[-3:] //Printing the last 3 elements
print(season_global1)
print(season_global2)

Output:

['autumn', 'Summer', 'Winter']
['Spring', 'fall', 'monsoon']

Hope this tutorial has helped you understand the concept of slicing notation and how we can slice a list in a Python program.

Happy Coding!!!

You can also read, How to run a Python file in Linux – Step-by-Step Guide

Leave a Reply

Your email address will not be published. Required fields are marked *