Write a Python Program to Slice Lists

Slicing is a common operation performed on lists in Python.

It involves selecting a portion of the list and creating a new list with that portion.

Python makes slicing lists easy, as it provides a simple syntax for the operation.

In this tutorial, we will learn how to slice lists in Python.


The syntax for slicing a list is as follows:

new_list = old_list[start:stop:step]
  • start is the index of the first item that we want to include in the new list.
  • stop is the index of the first item that we do not want to include in the new list. This index is one more than the index of the last item we want to include.
  • step is the size of the increment between each item we want to include. This parameter is optional, and the default value is 1.

Let’s take a look at some examples to see how slicing works.

Example 1: Slicing a list with default values for start, stop, and step.

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

new_list = my_list[2:6]

print(new_list)  # Output: [2, 3, 4, 5]

In this example, we are slicing the list my_list from index 2 to index 6.

Since we did not specify a value for step, the default value of 1 is used.

Slicing a list with a step of 2

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

new_list = my_list[0:10:2]

print(new_list)  # Output: [0, 2, 4, 6, 8]

In this example, we are slicing the list my_list from index 0 to index 10, with a step of 2.

This means that we will select every other item in the list.

Slicing a list with negative indices

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

new_list = my_list[-5:-2]

print(new_list)  # Output: [5, 6, 7]

In this example, we are slicing the list my_list from the 5th last item to the 2nd last item.

Example 4: Slicing a list to create a copy.

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

new_list = my_list[:]

print(new_list)  # Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In this example, we are slicing the entire list my_list, which effectively creates a copy of the list.


Slicing lists in Python is a simple and powerful operation that can save a lot of time when working with lists.

By understanding the syntax and examples provided in this tutorial, you should be able to slice lists with ease in Python.