How to Slice List in Python
List slice in Python is a technique in which we can obtain a range of elements in the original list by specifying a range of index numbers separated by a colon. It is one of the most powerful features provided by a list in Python.
A portion of elements of an original list is called slice. It is basically a sequence of elements which already exists in the original list.
List slicing is a common operation in Python that we generally perform in the program. The method of slicing a list is the same as string slicing.
With list slicing, we can access a range of elements in a list or create a sublist. Let’s understand it with the help of an example program.
Example:
# Program to demonstrate the list slicing in Python.
# Original list.
list = [20, 40, 60, 80, 100, 120]
my_slice = list[0:4] # list slicing.
print("Sublist:",my_slice)
Output:
Sublist: [20, 40, 60, 80]
In the above code, we have obtained a sub-list of four elements starting from 0 to 4. We have specified the starting and ending indices of the slice separated by a colon in the square brackets ([ ]), which is an array operator.
The starting (i.e. first) index is inclusive, and the ending (i.e. last) index is exclusive. So, 0:4 means 0 to 3 sequence of elements, not the 4th element. Look at the below figure to understand list slicing better.
Ways to Slice List in Python
There are basically two ways to slice a list in Python. They are as:
- Using slice() method (Built-in method)
- Using array slice operator
Let’s understand each technique one by one with the help of some examples.
List Slicing by slice() Function
Python language provides a built-in function named slice() that we can use to obtain a slice (i.e. subpart) of elements from the list.
This function returns a slice object we can use to slice a sequence, such as strings, lists, tuples, or range objects. Python language provides two overloaded slice() functions.
The first function takes a single formal parameter ‘stop’ index, while the second function takes three formal parameters ‘start’ index, ‘stop’ index, and ‘step’ value as well.
The basic syntax for slice() function with parameters is as follows:
slice(stop)
slice(start, stop, step)
Parameters:
(a) A start parameter in the second syntax specifies the starting index at which the slicing of list object begins.
(b) A stop parameter specifies the ending index where a list object’s slicing comes to a halt. The slice() function excludes the stop index while generating the sub-list. That is, the slice() doesn’t stop ‘at’ or ‘after’ the stop index. It stops just before this index when performing the slice.
(c) A step parameter in the second syntax is an optional parameter that specifies the increment between each index for slicing. It basically refers to the number of elements we can skip after the start indexing element in the list.
Return Type:
Both slice() functions return a sliced object containing elements in the specified range only. In other words, they create a slice object that contains a set of ‘start’ & ‘stop’ indices and step values.
Let us take a simple example program in which we will slice a list in Python using slice() function.
Example 1:
# Program to demonstrate the list slicing.
# Original list.
my_list = [1, 3, 6, 9, 12, 15, 18, 21]
# This statement starts the slice object at index position 0 and slice up to position 5 and returns the result.
my_slice1 = slice(5) # returns slice object.
print('Sublist from 0 to 5:',my_list[my_slice1])
# This statement starts the slice object at index position 2 and slice up to position 6 and returns the result.
my_slice2 = slice(2, 6) # returns slice object.
print('Sublist from 2 to 6 :',my_list[my_slice2])
# This statement uses the step parameter to return every second element.
my_slice3 = slice(0, 7, 2)
print('Sublist containing every second elements:',my_list[my_slice3])
Output:
Sublist from 0 to 5: [1, 3, 6, 9, 12]
Sublist from 2 to 6 : [6, 9, 12, 15]
Sublist containing every second elements: [1, 6, 12, 18]
Explanation: In the above program code,
(a) First, we have created a list of elements and stored them in a variable my_list. We then have called the slice() function by passing an argument 5 to the ‘stop’ parameter.
The slice() starts the slice object at position 0 and slice up to index position 5 and returns the result as “[1, 3, 6, 9, 12]”. It automatically sets the ‘start’ index to 0. We have stored the returned result in a variable named my_slice1.
(b) After that, we have invoked slice() function by passing argument values 2 and 6 for ‘start’ and ‘stop’ indices, respectively. The slice() function creates a slice() object containing a sub-list from the index position 2 up to the position 6 and returns the result as “[6, 9, 12, 15]”. We have stored the returned result in a variable my_slice2 and displayed it on the console.
(c) In the third function calling, we have also passed the step argument 2. It returns a sub-list of every second element, starting from index 0 to 7. We have stored returned value in a variable named my_slice3 and printed the result on the console.
Python List Slicing using slice operator
We can obtain a sub-list of an original list using slice operator. It is an easy and convenient way to slice a list in Python. To slice or access a range of elements in a list or create a sublist, use the slice operator [:].
The general syntax for slicing a list is as follows:
list_name[start : end[: step]]In the above syntax, a colon specifies the range of value. It also separates the two indices. We can access a portion of elements within an original list by specifying a range of index number by colon.
This slice syntax returns the sequence or a portion of elements beginning at the start index and extending up to, but not including, the end index. The start and end values should be an integer. We can perform list slicing operation using either positive or negative indexing.
Let’s take an example in which we will perform list slicing operation using range slice operator.
Example 2:
# Python program to demonstrate the list slice using range slice operator.
# Original list.
fruits_list = ["Apple", "Orange", "Banana", "Guava", "Papaya", "Mango"]
print('List of fruits:')
print(fruits_list)
# This statement starts to slice string at index position 0 and slices up to position 4 and returns the result.
my_slice1 = fruits_list[0:4]
print('Sublist from 0 to 4:',my_slice1)
# This statement starts to slice string at index position 0 and slices up to position 5 and returns the result.
my_slice2 = fruits_list[:5]
print('Sublist up to 5:',my_slice2)
# This statement starts list slicing at position 3 and slices up to end.
my_slice3 = fruits_list[3:]
print('Sublist starting from 3 and up to end:',my_slice3)
# This statement does not slice the list and displays the entire list.
my_slice4 = fruits_list[:]
print('Entire sublist:',my_slice4)
# This statement produces an empty list.
my_slice5 = fruits_list[4:4]
print('Empty list:',my_slice5)
# This statement starts to slice the list at position 2 and slices up to end position (including).
my_slice6 = fruits_list[2:6]
print('Sublist up to end:',my_slice6)
Output:
List of fruits:
['Apple', 'Orange', 'Banana', 'Guava', 'Papaya', 'Mango']
Sublist from 0 to 4: ['Apple', 'Orange', 'Banana', 'Guava']
Sublist up to 5: ['Apple', 'Orange', 'Banana', 'Guava', 'Papaya']
Sublist starting from 3 and up to end: ['Guava', 'Papaya', 'Mango']
Entire sublist: ['Apple', 'Orange', 'Banana', 'Guava', 'Papaya', 'Mango']
Empty list: []
Sublist up to end: ['Banana', 'Guava', 'Papaya', 'Mango']
Explanation:
In the above example, we have created a variable named fruits_list and assigned a list of fruits.
(a) In my_slice1, we have sliced the original list from the position 0 up to position 4 and stored the returned result in a variable my_slice1.
(b) In my_slice2, we have not included the start index number. Therefore, slicing starts from position 0 and goes up to index number 5. We have stored the returned result in a variable my_slice2.
(c) In my_slice3, we have not included the stop index value. Therefore, slicing starts at index position 3 and slices the original list up to the end of list index.
(d) In my_slice4, we have not omitted the start and stop index value. So, the entire list will display.
(e) If the start index number is equal to or higher than end index number. In my_slice5, the start index number is equal to end index number. Therefore, it displays an empty list.
(f) In my_slice6, the end index number is beyond the end of list. Therefore, it stops at the end of list.
List Slicing using slice operator with Negative index
We can also access a range of elements of a list using negative index. A negative index number begins from -1 from the end of a list that represents the last element of the list.
The index decreases by one as we move from right to left. For example, -2 represents the second last element and so on. While working with negative index, specify the lowest negative integer number in the start index position.
Example 3:
# Python program to demonstrate how to slice list using negative index.
veg_list = ["Tomato", "Onion", "Potato", "Turnip", "Spinach", "Ginger"]
print('List of vegetables:')
print(veg_list)
# This statement begins to slice list at position -4 and slices up to position -1 and returns the result.
my_slice1 = veg_list[-4:-1]
print('Sublist from -4 to -1:',my_slice1)
# This statement begins to slice list at position -6 and slices up to position -4 and returns the result.
my_slice2 = veg_list[-6:-4]
print('Sublist from -6 to -4:',my_slice2)
# This statement begins the list slice at position -6 and slices up to end.
my_slice3 = veg_list[-6:]
print('Sublist up to end:',my_slice3)
# This statement produces an empty list.
my_slice4 = veg_list[-4:-4]
print('Empty list:',my_slice4)
my_slice5 = veg_list[-3:-9]
print('Sublist:',my_slice5)
Output:
List of vegetables:
['Tomato', 'Onion', 'Potato', 'Turnip', 'Spinach', 'Ginger']
Sublist from -4 to -1: ['Potato', 'Turnip', 'Spinach']
Sublist from -6 to -4: ['Tomato', 'Onion']
Sublist up to end: ['Tomato', 'Onion', 'Potato', 'Turnip', 'Spinach', 'Ginger']
Empty list: []
Sublist: []
We can also combine positive and negative indexing numbers to access a range of elements in a list. Let’s take a simple example program based on it.
Example 4:
# Python program to slice list using positive and negative indices.
list = [0, 5, 10, 15, 20, 25, 30]
print('Original list:',list)
my_slice1 = list[4:-1]
print('Sublist from 4 to -1:',my_slice1)
my_slice2 = list[2:-2]
print('Sublist from 2 to -2:',my_slice2)
Output:
Original list: [0, 5, 10, 15, 20, 25, 30]
Sublist from 4 to -1: [20, 25]
Sublist from 2 to -2: [10, 15, 20]
In the above slice operation, we have not specified the step argument along with the start and stop arguments. In its absence, Python uses the default value 1. Therefore, we can also specify a third argument ‘step’ (optional) along with the start and stop indexing numbers.
The argument ‘step’ refers to the number of elements that can be skipped after the start indexing element in the list.
Example 5:
# Python program to slice list with start, stop, and step arguments.
list = [0, 5, 10, 15, 20, 25, 30]
print('Original list:',list)
my_slice1 = list[0:7:2]
print('Sublist containing every second element :',my_slice1)
my_slice2 = list[::3]
print('Sublist containing every third element:',my_slice2)
Output:
Original list: [0, 5, 10, 15, 20, 25, 30]
Sublist containing every second element : [0, 10, 20, 30]
Sublist containing every third element: [0, 15, 30]
Explanation:
(a) In my_slice1, the slice [0:16:2] takes the element at position 0 and will print every second element in the list extending till 7th element (excluding).
(b) In my_slice2, we have omitted both start and end index values to consider the entire range of elements in the list by specifying two colons. We have specified step argument 3 to skip elements.
In this tutorial, you have learned about list slice in Python with the help of examples. I hope that you will have understood the basic concept of list slicing and practiced all example programs.
Thanks for reading!!!





