Examples

List Slicing in Python

We may have encountered instances in which we needed to access only a portion of the data, a subset of a text, or a few entries of a list. This is where the idea of Data Slicing comes into play. The process of accessing a portion/subset of a given data sequence is known as data slicing. Data slicing can also be used to slice a huge dataset into smaller bits for faster processing. List slicing is a frequent practice in Python, and it is the most commonly utilized way for programmers to solve efficient problems. In this tutorial, you’ll learn how to retrieve specific object elements and sequences within your lists.

Python 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. Let us consider a Python list. To access a range of elements in a list, you must slice it. One method is to utilize the simple slicing operator, i.e. colon (:) With this operator, one can define where to begin slicing, and where to terminate slicing, and the step. List slicing creates a new list from an old one.

Syntax

                    

list[start: stop: step]

where,

  • start – index position from where the slicing will start in a list
  • stop – index position till which the slicing will end in a list
  • step – number of steps, i.e. the start index is changed after every n steps, and list slicing is performed on that index

Note – In Python, indexing starts from 0, and not 1.

Get all the Items

                    

my_list = [1, 3, 5, 7, 9]

print('Items in List are:', my_list[:])

Output

                    

Items in List are: [1, 3, 5, 7, 9]

In the above program, to get all the elements of the list we use ‘:’. This is similar to the statement print(my_list)

Get all the Items After a Specific Position

                    

my_list = [1, 3, 5, 7, 9, 11, 13, 15]

print('Items in List after 3rd position are:', my_list[3:])

Output

                    

Items in List after 3rd position are: [7, 9, 11, 13, 15]

In this program, we have used the start parameter by specifying an integer value that indicates the starting position of the slicing of the list. The elements at index 3 and all the elements after index 3 will be displayed.

Get all the Items Before a Specific Position

                    

my_list = [1, 3, 5, 7, 9, 11, 13, 15]

print('Items in List till 4th index are:', my_list[:4])

Output

                    

Items in List till 4th index are: [1, 3, 5, 7]

In this program, we have used the stop parameter by specifying an integer value that indicates the ending position of the slicing of a list. The items before index 4 are sliced in the example. Element on index position 4 is not included.

Get all the Items from One Position to Another Position

                    

my_list = [1, 3, 5, 7, 9, 11, 13, 15]

print('Items in List from 1st to 6th index are:', my_list[1:6])

Output

                    

Items in List from 1st to 6th index are: [3, 5, 7, 9, 11]

If you wish to display all the elements between two specific indices, put them before and after the ‘:’ symbol. In the preceding example, my list[1:6] returns the elements between the first and sixth positions. The beginning position (i.e. 1) is included, but the finishing position (i.e. 6) is not.

Get the Items at Specified Intervals

                    

my_list = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

print('Items in List at step of 2 are:', my_list[::2])

Output

                    

Items in List at step of 2 are: [1, 5, 9, 13, 17]

In this type, we have made use of the step parameter for slicing. The step parameter is an integer value that prints the list elements after specific intervals. For example, in the above program, we have declared a step of 2. So the elements in the position of 0, 2, 4, 6, and so on will be printed.

If you want the indexing to begin with the final item, we can use the negative sign ‘-‘. In the below example, Items at interval 2 beginning with the last index are sliced.

                    

my_list = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

print('Items in List are:', my_list[::-2])

Output

                    

Items in List are: [19, 15, 11, 7, 3]

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.