Sayantan's Blog On Python Programming

STEP SIZE IN PYTHON LIST SLICING

STEP SIZE IN PYTHON LIST SLICING

INTRODUCTION

Step size allows us to put filter on sliced data.

POSITIVE STEP SIZE IN LIST SLICING

Syntax : [start_index : end_index : setp_size]

# Slicing list elements with step size example 1

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[0:6]
print("\nElement of sliced list ->", int_list_slice)

int_list_slice = int_list[0:6:]
print("\nElement of sliced list with step size ->", int_list_slice)

STEP SIZE IN PYTHON LIST SLICING : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [400, 200, 600, 100, 150, 500]

Element of sliced list with step size -> [400, 200, 600, 100, 150, 500]

# Slicing list elements with step size example 2

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[0:6]
print("\nElement of sliced list ->", int_list_slice)

int_list_slice = int_list[0:6:2]
print("\nElement of sliced list with step size ->", int_list_slice)

STEP SIZE IN PYTHON LIST SLICING : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [400, 200, 600, 100, 150, 500]

Element of sliced list with step size -> [400, 600, 150]

In the above example, we have used the complete list in the slicing operation but then have used the step size as 2. Step size 2 indicates that every alternative elements will be returned. That’s why 400, 600, 150 has been returned.

# Slicing list elements with step size example 3

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[0:4]
print("\nElement of sliced list ->", int_list_slice)

int_list_slice = int_list[0:4:2]
print("\nElement of sliced list with step size ->", int_list_slice)

STEP SIZE IN PYTHON LIST SLICING : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [400, 200, 600, 100]

Element of sliced list with step size -> [400, 600]

# Slicing list elements with step size example 4

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[2:4]
print("\nElement of sliced list ->", int_list_slice)

int_list_slice = int_list[2:4:2]
print("\nElement of sliced list with step size ->", int_list_slice)

STEP SIZE IN PYTHON LIST SLICING : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [600, 100]

Element of sliced list with step size -> [600]

# Slicing list elements with step size example 5

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[0:5]
print("\nElement of sliced list ->", int_list_slice)

int_list_slice = int_list[0:5:3]
print("\nElement of sliced list with step size ->", int_list_slice)

STEP SIZE IN PYTHON LIST SLICING : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [400, 200, 600, 100, 150]

Element of sliced list with step size -> [400, 100]

In the above example, we have sliced the data up to index position 4. Then we have use the step size 3 which has filtered out every 3rd alternative elements of sliced data. That’s why 400 and 100 have been returned.

NEGATIVE STEP SIZE IN LIST SLICING

Python allows negative step sizing in list.

# Slicing list elements with negative step size example 1

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[:]
print("\nElement of sliced list ->", int_list_slice)

int_list_slice = int_list[::-1]
print("\nElement of sliced list with step size ->", int_list_slice)

STEP SIZE IN PYTHON LIST SLICING : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [400, 200, 600, 100, 150, 500]

Element of sliced list with step size -> [500, 150, 100, 600, 200, 400]

In the above example, we have used negative step size as -1. It will sorted the list in reverse order.

# Slicing list elements with negative step size example 2

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[:]
print("\nElement of sliced list ->", int_list_slice)

int_list_slice = int_list[::-2]
print("\nElement of sliced list with step size ->", int_list_slice)

STEP SIZE IN PYTHON LIST SLICING : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [400, 200, 600, 100, 150, 500]

Element of sliced list with step size -> [500, 100, 200]

In the above example, we have used step size as -2. So it has listed out second alternative elements from the reverse direction.

Till now we have used the entire list for step size operation. Now we will use the shorter range in list.

In order to use the negative step size we have to use the higher start index and lower end index. Otherwise, python will return empty list.

# Slicing list elements with negative step size example 3

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)

int_list_slice = int_list[0:3:-2]
print("\nElement of sliced list with step size ->", int_list_slice)

STEP SIZE IN PYTHON LIST SLICING : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list with step size -> []

In the above example, we have used lower start index and higher end index, that’s why python has returned empty list. Because, python can not go from 0 to 3 in reverse order. It can go from 3 to 0 in reverse order as we can view in the next example.

# Slicing list elements with negative step size example 4

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)

int_list_slice = int_list[3:0:-2]
print("\nElement of sliced list with step size ->", int_list_slice)

STEP SIZE IN PYTHON LIST SLICING : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list with step size -> [100, 200]

In the above example, the start index is 3 which indicates the element 100. End index is 0 (exclusive) which indicates the element 200. Now, step size -2 will return every 2nd alternative element starting from 100 in reverse order. That’s why 100 and 200 has been returned by python.

# Slicing list elements with negative step size example 5

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)

int_list_slice = int_list[-4:0:-1]
print("\nElement of sliced list with step size ->", int_list_slice)

STEP SIZE IN PYTHON LIST SLICING : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list with step size -> [600, 200]

In the above example, we have seen that starting index is -4 which indicates the element 600 and end index is 0 (exclusive) which indicates the element 200. Now, step size is -1. So, every element will be listed starting from 600 to 200. That’s why 600 and 200 has been returned by python.

USING SLICING OPERATION TO CREATE NEW LIST ELEMENTS

Below is the way we can insert new elements in a list.

# Adding new list elements using Slicing operation example 1

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)

int_list[6:0] = [750, 850]
print("\nRevised element of original list ->", int_list)

STEP SIZE IN PYTHON LIST SLICING : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Revised element of original list -> [400, 200, 600, 100, 150, 500, 750, 850]

In the above example, we have used the slicing operation to add new elements in the list. The above list initially has 6 elements and highest index position was 5. So we have used index position 6 as the starting index and end index is optional. We can put any value as end index including null. Two new elements are added in the list.

# Adding new list elements using Slicing operation example 2

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)

int_list[6:] = [750, 850, 900]
print("\nRevised element of original list ->", int_list)

STEP SIZE IN PYTHON LIST SLICING : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Revised element of original list -> [400, 200, 600, 100, 150, 500, 750, 850, 900]

In the above example, we have used null as end index, then also 3 new elements are added in the list.

STEP SIZE IN PYTHON LIST SLICING : Output
STEP SIZE IN PYTHON LIST SLICING : Output

RELATED TOPICS:

Leave a Comment

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