/  Technology   /  Python |Understanding Slice Notation On List
Python |Understanding Slice Notation On List

Python |Understanding Slice Notation On List

First, we will have a grasp of indexing for sequential types and then discuss slice notation.

Let’s take a simple example:

>>> colors = [‘red’, ‘green’, ‘blue’, ‘yellow’, ‘white’, ‘black’]

In the above example we have defined a list of colors. Python uses zero-based indexing which means, the first element has a 0 index, the second has index 1, and so on. Below image shows the representation of indexing.

 

Python |Understanding Slice Notation On List

 

We use square brackets to access an element by its index:

 

Example:

 

>>> colors = ['red', 'green', 'blue', 'yellow', 'white', 'black']
>>>colors[0]
'red'

 

Negative indexes

 

In this case, we can access the elements from the tail of a list and we call as negative indexing. Here instead of indexing the elements from zero and above, we can use indexing from -1 and below. Below image shows representation of negative indexing.

 

Python |Understanding Slice Notation On List

 

In negative indexing -1 points to the last element of the list, -2points to second last element that is white and so on.

 

Example:

 

>>> colors = ['red', 'green', 'blue', 'yellow', 'white', 'black']
>>>colors[-1]
'black'
>>>colors[-6]
'red'

 

Slice Notation

 

Let’s take a basic list:

 

>>>nums = [100, 200, 300, 400, 500, 600, 700, 800, 900]

 

Below is an example to take a sublist from the nums list.

 

>>>nums = [100, 200, 300, 400, 500, 600, 700, 800, 900]
>>>some_nums = nums[2:7]
>>>some_nums
[300, 400, 500, 600, 700]

 

The full slice syntax is: start:stop:step.start refers to the starting index of the element our slice. stop refers to the index of the element where we need to finish slice. step lets you to access each nth-element within a start:stop range.

Below is an example of slicing where we can extract an arbitrary part of a list.

 

Example:

 

>>>nums = [100, 200, 300, 400, 500, 600, 700, 800, 900]
>>>nums[0:4]
[100, 200, 300, 400]

 

 Taking n first elements of a list

 

Using slice notation allows you can skip any element of the complete syntax.

 

Example:

 

>>>nums = [100, 200, 300, 400, 500, 600, 700, 800, 900]
>>>nums[:5]
[100, 200, 300, 400, 500]

 

In the above examplenums[:5] is equivalent to nums[0:5].

 

Taking n last elements of a list

 

Negative indexing also allow you to easily access n-last elements of a list:

 

Example:

 

>>>nums = [100, 200, 300, 400, 500, 600, 700, 800, 900]
>>>nums[-3:]
[700, 800, 900]

 

We can easily use combination of negative and positive indexes in start and stop positions.

 

Skipping n last elements of a list

 

This is another good usage of negative indexing. Using this slicing operation you can skip n last elements of a list.

 

>>>nums = [100, 200, 300, 400, 500, 600, 700, 800, 900]
>>>nums[:-2]
[100, 200, 300, 400, 500, 600, 700]

 

Taking every nth-element of a list

 

In order to access every n-th element of a list step parameter comes into play.

 

Example:

 

>>>nums = [100, 200, 300, 400, 500, 600, 700, 800, 900]
>>>nums[::2]
[100, 300, 500, 700, 900]

 

Above example skips every second element of nums list.

 

Using Negative Step and Reversed List

 

We can implement negative step to access the reversed list:

 

Example:

 

>>>nums = [100, 200, 300, 400, 500, 600, 700, 800, 900]
>>>nums[::-1]
[900, 800, 700, 600, 500, 400, 300, 200, 100]

 

In this article we discussed two key list operations they are indexing and slicing. These both concepts are vital to use Python efficiently.

 

 

 

 

Leave a comment