Javatpoint Logo
Javatpoint Logo

Python slice() Function

In Python, we have a number of inbuilt functions. One such function is the Python slice() function. The Python slice function is used to get a slice or a portion of elements from the collection of elements such as a list, tuple, or string.

The slice function offers a simple and effective method for extracting a portion of data and modifying data from a sequence of elements.

You can use the slice function to encapsulate slice logic, such as start, stop, and step parameters.

Python provides two overloaded slice functions. The first function takes a single argument, while the second function takes three arguments and returns a slice object. This slice object can be used to get a subsection of the collection. For example, if we want to get the first two elements from the list of elements, here slice can be used.

Introduction to Slicing:

Before we deep dive into the Python slice function. We must understand the concept of slicing.

Slicing is a process of extracting data from a collection of data by specifying the starting and ending indices and steps to be taken.

The general syntax of slicing is as follows:

The collection can be any list, string, or tuple (an iterable). The start parameter denotes the starting index, the end parameter denotes the ending index, and the step parameter denotes the number of elements to be skipped during the slice operation.

Introduction to Python Slice Function:

Signature

The signature of the slice function is given below.:

Parameters

  • start: The start parameter in the slice function is used to set the starting position or index of the slicing. The default value of the start is 0.
  • stop: The stop parameter in the slice function is used to set the end position or index of the slicing.
  • step: The step parameter in the slice function is used to set the number of steps to jump. The default value of the step is 1.

The start, stop, and step parameters are similar to those used in the slicing syntax, and it returns a slice object.

Let's see some examples of slice() function to understand its functionality.

Python slice() Function Example 1 - Creating a slice object

Output:

slice(None, 5, None)
slice(0, 5, 3)

In this example, we create two slice objects using slice(5) and slice(0, 5, 3). The slice(5) represents a slice that starts from index 0 and ends at index 5 (exclusive). And the slice(0, 5, 3) represents a slice that starts from index 0, ends at index 5 (exclusive), and selects every third element from the sequence.

Python slice() Function Example 2 - Using a slice object

Let's use this slice object to extract data from a list of elements:

Output:

Slice 1 of My list: [1, 2, 3, 4, 5]
Slice 2 of My list: [1, 4]

In the above example, the slice1 object extracts the portion of data from the my_list starting from index 0 and ending at index 4. And the slice2 object extracts the portion of data from the my_list starting from index 1 and selects every third element from the index.

Python slice() Function Example 3 - Slicing string

Output:

Result 1: Jaot
Result 2: toa

Explanation:

In the above example, slice1 represents a slice object that starts extracting data from index 0 and extracts every third element from the string ending at index 10 (exclusively).

And slice2 represents a slice object that starts extracting data from the last index (index = -1) and extracts every third element in reverse order ending at index 0 (exclusively).

Python slice() Function Example 4 - Slicing Tuple

Output:

Result 1: (45, 1214, 636)
Result 2: (66, 41, 68)

Explanation:

As we discussed in the previous example, the slice1 object extracts every third element from 'tup' starting from index 0 and ending at index 10 (exclusively).

And the slice2 object extracts every third element starting from index -1 and ending at index 0 (exclusively).

Python slice() Function Example 5 - General Slicing vs. Slice Function

Output:

Slicing using slice object: (45, 1214, 636)
Slicing using general slicing: (45, 1214, 636)

Explanation:

In the above example, both slicing methods extract every third element from the 'tup' starting from index 0 and ending at index 10 (exclusively).

Advantages of Slice() function in Python:

In Python, the slice function offers many advantages while working with sequences or collections of data such as lists, tuples, or strings. Some of the main advantages of the slice function are as follows:

  1. Reusability - Once created, the slice object can be applied to slice many sequences making it advantageous in terms of reusability.
  2. Readability - The slice object is used to encapsulate the slicing logic, which enhances the readability of the code.
  3. Flexibility - The slice function takes three parameters start, stop, and steps. You can leave any of these parameters or use negative indices for the same.

Conclusion:

The slice function in Python is a powerful tool that provides an efficient and flexible way to extract data from a sequence. The slice object is reusable, one can use the slice function to encapsulate the slice logic start, stop, and step parameters and create a slice object based on the logic that can be used to extract data from a number of sequences. Using the slice function or object makes the code more readable. Understanding the flexible way of slicing can simplify the manipulation of data.


Next TopicPython Set





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA