What is Python slice?

A visual tutorial on slicing and dicing data with Python. Discover the flexibility of slice notation and slice objects for customizing data extraction.

  • Slicing is a Python methodology that enables accessing parts of data from the given sequence like lists, tuples, strings, etc., and objects that support sequence protocol.
  • A slice object is created based on a set of indices (specified by range) as using the slice constructor where you may specify the start, stop, and step indices.

How to use the slice object?

Following is the general syntax of using the slice:

slice(start ,stop ,step)

There is another way to slice objects in Python i.e. the extended slicing. It uses the colons syntax i.e.

object[start:stop:step]

Where an object can be a list, string, tuple, etc.

This way is explained in the later part of this tutorial, so keep reading.

An example of slicing a list

In the first example of demonstrating Python slicing, a slice object is created with all three parameters with positive indices.

A list of a few items is created and finally, a print function is used where that slice object is used to extract the list items based on the slicing object. See the code and output below:

#A demo of slicing objects



a_List = ['S', 'l', 'i', 'c', 'i', 'n', 'g']

obj_slice = slice(1, 5, 1)



print(a_List[obj_slice])

Output:

[‘l’, ‘i’, ‘c’, ‘i’]

The example of slice with string for getting a substring

In the following example, a string is created and a slice object is used to get the substring of the source string. This time, only two parameters are used in the slice i.e. start and stop while the step is not given.

#A demo of slicing objects

str_x = "The slice Tutorial"

obj_slice = slice(4, 18)

print("The substring after slice:" ,str_x[obj_slice])

Output:

The substring after slice: slice Tutorial

What if a single parameter is provided?

If you provide only the single parameter then it is taken as the stop value. In that case, the start and end parameters are set to None.

See this example where a tuple is created with numeric objects. The slice object is created with a single parameter. See the code and what it outputs:

#A demo of slicing objects

a_tuple = (10,20,30,40,50,60)

obj_slice = slice(4)



print("The tuple after slice:" ,a_tuple[obj_slice])

Output:

The tuple after slice: (10, 20, 30, 40)

From the output, you can see the slicing started at 0 index with the step of 1 and ended at the given value i.e. 4. So, after slicing, the print displayed 4 elements from the source tuple.

Reversing the order by negative values

By using the negative values for start, stop, and step parameters, you may reverse the order of slicing for the given sequence.

A value of -1 for the start means start from the last of the sequence.

See this example where a list is created and negative numbers are used to slice the list.

#A demo of slicing object with negative numbers

a_List = [5,13,19,23,29,37]

obj_slice = slice(-1,-4, -1)



print("List before slicings:" ,a_List)

print("The List after negative values for slice:" ,a_List[obj_slice])

Output:

List before slicings: [5, 13, 19, 23, 29, 37]
The List after negative values for slice: [37, 29, 23]

You see the order of the list is reversed after using the slice object with negative indices.

The extended slicing way

The other way of slicing the objects like tuple, lists, arrays etc. is using the extended slice. The syntax is as follows:

object[start:stop:step]

So unlike slice object parameters, the start, stop and step are separated by colons. See the examples below.

Slicing the list example

All parameters are used in this example for slicing a list of numeric items. The values for the start, stop and step are positive:

#A demo of extended slicing

a_List_slice = [10,20,30,40,50,60,70]



print("Original List:", a_List_slice)

print("Sliced List:", a_List_slice[2:6:1])

Output:

extended slice

Using only one value example

If you omit the last two values or those are None, the rest of the items from the given sequence will return. See the code with the same list as used in the above example:

a_List_slice = [10,20,30,40,50,60,70]



print("Original List:", a_List_slice)

print("Sliced List:", a_List_slice[1::])

Output:

Original List: [10, 20, 30, 40, 50, 60, 70]
Sliced List: [20, 30, 40, 50, 60, 70]

What if none of the value is provided?

It will return the whole copy of the sequence. See an example of a tuple:

a_tuple_slice = (1,2,3,4,5,6)



print("Original Tuple:", a_tuple_slice)

print("Sliced Tuple:", a_tuple_slice[:])

Output:

Original Tuple: (1, 2, 3, 4, 5, 6)
Sliced Tuple: (1, 2, 3, 4, 5, 6)

Reversing the order of sequence by using a negative value

By using the negative value, you may reverse the order of sequence for slicing. If you want to count from the last item and return a complete list, tuple, etc., then use the -1 value for the step:

a_tuple_slice[::-1]

Use the negative numbers for start and end values for getting the subset e.g.

a_tuple_slice[-1:-4:-1]

This will return last three items as shown below:

a_tuple_slice = (1,2,3,4,5,6)



print("Original Tuple:", a_tuple_slice)



print("Sliced Tuple with [::-1] = ", a_tuple_slice[::-1])

print("Sliced Tuple with [-1:-4:-1] = ", a_tuple_slice[-1:-4:-1])

Output:

extended slice reverse

In the output figure, you can see the counting starts from the last item.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!