List Slicing in Python

What is Data Slicing?

Data slicing is the process of accessing a portion or subset of a data stream. This technique is useful when we need to test some sample code, debug the code, or look for logical problems on a large dataset that consumes a lot of memory and takes a long time to execute. In this scenario, we can utilise data slicing to extract a subset of data from the entire dataset and use it for testing.

Data slicing can also be used to slice a huge dataset into smaller bits for faster processing. In short, there are many applications where smaller datasets make the code more efficient, reducing execution time, memory, and screen time.

What is List Slicing?

List slicing is the process of accessing a specified portion or subset of a list for some action while leaving the rest of the list alone. Python's slicing operator accepts three parameters, two of which are optional depending on the situation.

A list slice is a portion of a list that has been extracted and contains the necessary elements. The list slice is a list. A list slice can execute all list operations. List slices are used to copy the required elements to a new list and to alter the list's required components.

List slicing is the process of accessing a specific section or subset of a list for some activity while leaving the remainder of the list alone. Consider a Python list. To access a range of elements in a list, you must slice it. One option is to use the simple slicing operator, i.e. colon (:) With this operator, you can specify where to start slicing, where to stop slicing, and the step. List slicing produces a new list from an existing one.

Syntax of list slicing

list_name[start:stop:steps]

The start argument is required, although the stop and steps parameters are both optional. The start index represents the index from which the list slicing should begin. Its default value is 0, indicating that it starts at index 0.

The stop index represents the final index up to which the list slicing will proceed. Its default value is (length(list)-1) or the index of the list's final element.

List Slicing can be done in three ways

  • By providing only the start or stop parameters
  • By passing the parameters start and stop
  • By passing the parameters start, stop, and steps

Passing the start or stop index only

my_first_list = [1,2,3, 'Studytonight', 1.23]

print(my_first_list[-1]) 

# The index of the last element is usually the length of the data structure subtracted by 1. This can be represented as index -1 too. Hence indexing the list with -1 displays the last element present in the list.

print(my_first_list[-3]) 

# On the similar lines like in previous code, the third element from the last index can be accessed by providing the 'start' index as -3.

print(my_first_list[3:]) 

# This indicates that the 'start' index is 3, but the 'stop' index has been left blank.In such cases, the default value of the 'stop' index is considered.This indicates that all the element starting from 3rd index up to the last index to be displayed.

print(my_first_list[ : 4]) 

# Here, the 'start' index is empty, indicating its default value is to be considered while slicing the list. The 'stop' index has been mentioned as the 4th index.Here is the catch - the last index must be excluded. This means elements up to 3rd index only will be displayed.

Output

1.23
3
['Studytonight', 1.23]
[1,2,3, 'Studytonight']

write your code here: Coding Playground

Passing the start and stop index

my_first_list = [1,2,3, 'Studytonight', 1.23]

print(my_first_list[1 : 4]) 

# This indicates that the start index is 1 and the end index is 4,but the 4th element is excluded.

print(my_first_list[-3:-1]) 

# It indicates that the first element to be displayed is the third element from the end. Beginning from that index, go all the way up to the last element (-1) excluding the last element.

print(my_first_list[0:6])   

# This indicates that the 'start' index is 0 and the 'stop' index is 6, meaning beginning from the 0th index, display elements up to and excluding the 6th index element.

Output

[2, 3, 'Studytonight']
[3, 'Studytonight']
[1, 2, 3, 'Studytonight', 1.23]

Passing the step parameter with colons

my_first_list = [1,2,3, 'Studytonight', 1.23]

print(my_first_list[ :: -1]) 

# This indicates that elements should be displayed beginning from the last index (notice the 2 semi-colons, instead of one). Since no value for the 'start' index has been specified, it takes the default value of 0 index and displays elements in the reverse order.

print(my_first_list[ :: -2]) 

# Since two semi-colons are present, this indicates that the elements should be displayed in the reverse order, beginning from the last element. The -2 after the 2 semi-colons means display elements beginning from the last index then the second element from the end and so on(every second element). Remember to start counting from -1 index. Hence, the last element is displayed. Counting from the last element, 2 elements backwards would give 3. And two elements backwards starting from 3 would give 1.

print(my_first_list[ :: 2]) 

#This is just the opposite of the previous code, wherein the index indicates that the list should be displayed from the beginning index, i.e 0. After this, count 2 elements ahead, beginning from 0, starting from 0th index element and move 2 indices ahead,which gives 3.

print(my_first_list[::])     

#Passing two colons to the list indicates that there is no limit on the start, stop and step indices. Hence, all the elements present in the list are displayed.

print(my_first_list[ : ])    

# This indicates no limit on the start and stop parameters, hence default values are considered and the entire list is displayed. 

write your code here: Coding Playground

Output

[1.23, 'Studytonight', 3,2,1]
[1.23, 3, 1]
[1, 3, 1.23]
[1, 2, 3, 'Studytonight', 1.23]
[1, 2, 3, 'Studytonight', 1.23]

write your code here: Coding Playground

Passing start, stop and step parameters

my_first_list = [1,2,3, 'StudyTonight', 1.23]

my_first_list[0 : 3 : 1]  

# The values for start index is 0, stop index is 3 and step index is 1. This means beginning from the 0th index, display every element (since step is 1),and go up to the 3rd index, but don't include the element at 3rd index

Output

[1, 2, 3]

write your code here: Coding Playground

Quick fact

List slicing only displays a subset of the original list. If you want to change the contents of the original list by slicing it or give the sliced list a new name, you must assign the sliced list to a new list variable.

my_first_list = [1,2,3, 'StudyTonight', 1.23]
# slicing list and overiding its original value
my_first_list = my_first_list[-1]
print(my_first_list)

my_list = [1,2,3, 'StudyTonight', 1.23]
# slicing list and storing the new sliced list in different variable
my_second_list = my_list[-1]
print(my_second_list)

Output

1.23
1.23

write your code here: Coding Playground

Always remember to give the list a unique name rather than using generic names such as list, tuple, or dictionary, as this will cause complications when calling functions on these data structures.

Conclusion

This comes to the end of the article.These are the various applications of list slicing that can be put to use according to one’s considerations and needs.