Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Do you know, in Python List is a data structure that can store a collection of items. It is mutable; hence, we can edit a list. In order to modify a list, we use slicing.
In this article, we will study how we can slice a list in Python. We will study its syntax, methods, and some common slicing patterns.
What is List Slice in Python?
When we want to extract a specific part of a list, we can use slicing. It is used to extract a subset of data from the list. Slicing of list creates a new list with the extracted elements.
Note: Slicing does not change the original list.
Syntax of List Slicing
In order to slice a list, we have to mention three attributes. The start, stop, and step attributes. The syntax to slice is:
list [ start : stop : step ]
Start: The start component determines the starting element of the slicing. This element is also included in the output.
Stop: The stop component determines the end point of the sliced list. It is not included in the sliced list.
Step: The step component determines the number of elements to skip while slicing a list.
Let us understand various methods of slicing.
Methods of slicing
Below are the methods of slicing.
Slicing with positive index
Positive slicing starts from the first element of the list.
In the above code, we created a my_list. We then created another list called reversed_list and stored all the values with a step of -1. Thus the reversed list is created. The output of above code is as follows:
Modifying a given list
List is mutable, thus we can add, remove and replace elements from a list. Let us see sample code to see how to use slicing to modify a list.
Code
Python
Python
my_list = [1, 2, 3, 4, 5, 6]
# Replacing the elements from index 2 to 4 with new values my_list[2:5] = [7, 8, 9] print(my_list)
# Delete elements from index 3 to the end of the list my_list[3:] = [] print(my_list)
# Insert elements at the end using slice my_list[2:2] = [3, 4, 5] print(my_list)
You can also try this code with Online Python Compiler
In the above example, we first replaced all elements from 2 to 4. Then we deleted all the elements from 3rd to the end. Finally, we added elements at the end of the list.
Slicing in Python
Slicing is not restricted to lists. In Python, we can apply slicing on tuples and strings as well. It is similar to how we do slicing in lists.
Slicing in Tuples
Let us see a code snippet to understand it better
Code
Python
Python
my_tuple = (1, 2, 3, 4, 5) print(my_tuple[1:4])
You can also try this code with Online Python Compiler
The first line extracts the last three elements from my_list using negative indexing. The second line retrieves elements from index 2 to 2 from the end, excluding the last two elements.
Uses of Python List slice
Below are the uses of Python list slice.
Extracting a subsequence: We can extract a specific portion of a list.
Copying a sequence: We can slice a portion of a list and create a copy list of that.
Modifying a subsequence: We can modify a specific portion of a list with the help of slicing.
Reversing a sequence: We can reverse a list with the help of slicing.
Skipping elements: With the help of slicing, we can skip some elements from a list by specifying the step value.
In Python, the operator [:] makes a copy of the entire list and returns a new list with the same elements.
When slicing in Python What does the 2 in [:: 2] specify?
The '2' in [::2] specifies a slice step size of 2, which chooses every second piece.
Can a list be sliced Python?
The Python syntax list[start:end:step] allows you to slice a list, hence the answer is yes. It enables the extraction of particular list segments.
Can we slice a dictionary in Python?
No, we cannot slice dictionaries in Python. It does not support slicing because it is an unordered collection of key-value pairs. Also, it is not indexed.
What is the default behavior if we omit the start, stop, or step values in slice notation?
If we remove the stop attribute, slicing will continue until the end of the list. If we remove the step value, slicing will use a step of 1(default step value).
Conclusion
Slicing allows us to extract, modify, or create new sequences based on mentioned steps. In this article, we studied about slicing in Python. We also studied about the syntax, methods and common patterns used in slicing. To know more about slicing, you can refer to string slicing in Python.