What is List Slicing in Python?
List slicing in python can be understood as the technique for programmers to solve efficient problems by manipulating the list as per requirements through list slicing notation. List slicing is considered a common practice in Python for accessing a range of elements from a list. List slicing in pythons works similar to the Python slice() function.
List slicing in python is done by the simple slicing operator i.e. colon(:). With the use of the operator one can specify where to start the slicing, where to end, and specify the step too. After the slicing is done, list slicing in python returns a new list from the existing list.
Syntax:
Consider a list as Lst, then the above expression for list slicing in python can be analysed as the portion of the list from index Initial to index End, with a step size IndexJump.
The basic format for list slicing in python is [start:stop
start is defined as the index of the list where the slicing needs to start. stop is defined as the index of the list where the slicing wants to ends. step is defined as the nth item that you want to allow and select within the specified range start to stop.
Before moving further we should first go through the list in python module where we studied - what is list in python and how can we create and manipulate lists with respect to our requirements.
Examples
Let us learn the implementation of the concepts of list slicing in pyton that we learned so far by diving into the below examples.
Get All The Items
Given below example explains how we can Get all the Items from a list.
Code:
Output:
Explanation: As we have not excatly sliced the list, and used simply : ,' we get all the elements of the list as shown above.
Get all the Items After a Specific Position
Given below example explains how we can Get all the Items After a Specific Position from a list.
Code:
Output:
Explanation:
Above example illustrates , if we want to call all the elements starting from a specific index, we can do so by mentioning the index before : as shown in example above. Here we have element at index 2 and all the elements after index 2 are printed.
It must be noted that indexing starts from 0 therefore, Item on the index 2 shall also be included.
Get all the Items Before a Specific Position
Given below example explains how we can Get all the Items Before a Specific Position from a list.
Code:
Output:
Explanation: The above example shows how we can get all the elements occuring before a specific index. We use the : colon to specify the last index after it. As seen above our list got sliced at the 2nd index, and as we know the index at the end is not included.
Get all the Items from One Position to Another Position
Given below example explains how we can Get all the Items from One Position to Another Position from a list.
Code:
Output:
Explanation:
Whenever we want the list to be sliced in manner that the value between two index can be obtained we use the process as shown in above example. here we have specified the start and end index to give the point at which the our output list must start and the point before which our list must stop.
Above gives a new list that have items between 2nd and the 4th positions. The new list, starting position (i.e. 2) is included and the ending position (i.e. 4) is excluded.
Get the Items at Specified Intervals
Given below example explains how we can Get the Items at Specified Intervals from a list.
Code:
Output:
Explanation:
Whenever we need a list which must have item from the parent list, at specified intervals we can get that thing done via using the ::. This way we are aksing our whole list to be printed. Now in order to specify the exact elements from the parent list we give the step as 2 as above to get the reuired elements. As above example states, the items at interval 2 starting from index 0 are sliced.
Using Python List Slice to Get the n-first Elements from a List
Given below example explains how we can use Python List slice to get the n-first elements from a list
Code:
Output:
Explanation:
Here we have obtained the first three elements of the parent list by slicing the list at the end position of 3. We need to make a note here that the my_list[:3] is equivalent to the my_list[0:3].
Using Python List Slice to Reverse a List
Given below example explains how we can use Python List slice to reverse a list
Code:
Output:
Explanation: Here we see that in order to get a sliced list which is a reverse version of the original we simply have to spefcify the step position as -1, that way we get all the elements of the list but in a reversed manner.
Using Python List Slice to Substitute Part a List
Given below example explains how we can use Python List slice to substitute part a list
Code:
Output:
Explanation:
The list slicing in python allows us to change the list element by updating the list at the specified position as shown above. As shown above, we see that the index position 0 , 1 were updated to black , white respectively by specifying the list with the values as shown in above example.
Using Python List Slice to Partially Replace and Resize a List
Given below example explains how we can use Python List slice to partially replace and resize a list
Code:
Output:
Explanation:
In above example we have not only slice it but we have slice it to partially replace and resize the original list. Here we first have printed the length of the original list that is, 7 in above case. Then we have changed the certain elements in the list, by the names of the colors and taken the resized value of the existing list.
Using Python list slice to delete elements
Given below example explains how we can use Python list slice to delete elements from a list
Code:
print(my_list)
Output:
Explanation:
We can make use of the del function to slice list form the original list as per requirement. Here we have sliced the list by first deleting the elements at the specific position and then printed the remaining items of the list as our new list.
Conclusion
- List slicing in python can be understood as the technique for programmers to solve efficient problems by manipulating the list as per requirements through list slicing notation.
- The basic format for list slicing in python is [start:stop:step].
- We saw above various scenarios where we understand how we can make the use of list slicing in python and get the items of the list that is needed as per our requirement.
- We can make the use of list slice to extract a sublist from the original parent list and modify it as per our requirement