Python

Python slice() Function

Python is a high-level language that is advanced and convenient. Using simple built-in functions, a beginner developer can easily accomplish his tasks. The built-in function in the Python programming language enables novice developers to write huge lines of code without facing any difficulty. In this article, we are going to provide details about the slice() function. The slice() function in the Python programming language is a commonly used technique among beginners as well as expert programmers to solve different problems.

What is a Python slice() Function?

List slicing is used to take a subset of the elements from the set. A subset of the collection is obtained using the slice() function. For example, we have a list of 5 items and we need the first two items from the list. Here, we will use the slice() function and extract only the first two items from the list. It is a very common way in the Python programming language to access a range of elements in a list.

Syntax of slice() Function

The syntax of the slice() function is very simple. See the syntax below:

The ‘slice()’ is the name of the function that will operate and it will return a slice object. It takes three arguments: start, stop, and steps. The ‘start’ and ‘steps’ arguments are optional which means they can be skipped. However, the ‘stop’ argument must be provided. The ‘start’ argument represents the starting point of the list from where the slicing needs to be started. The ‘steps’ represents the steps that should be taken while extracting the other items from the list. The ‘stop’ argument represents the last point where the slicing should stop and not extract other items next to it.

Let’s look at a few examples to better grasp how the Python programming language’s slice() method works.

Example 1:

In the first example, we will only provide the ‘stop’ argument as it is necessary to provide and we cannot skip it. See the code below:

lst = ("1""a""2""b""3""c""4""d")

slc = slice(2)

print(lst[slc])

If you note that we have not imported any library to use the slice() function. This is the beauty of Python, that you do not need to remember every library to use the basic functions. The compiler automatically utilizes the required library without specifically importing it by the developer.

The first line of the code contains the list of items in the ‘lst’ variable. It has 8 items. The second line of code has the slice() function with a ‘2’ value in it representing the ‘stop’ value. Remember that whenever only one argument is provided to the slice() function, it will represent the ‘stop’ argument. Here, the slice() function will stop at 2 positions in the list. The third line contains the print(lst[slc]) statement which is used to print the items sliced by the slice() function. Let us see the output of the program given below:

As you can see, the slice() function returned two items from the list since the ‘stop’ number provided was ‘2’.

Example 2:

Now, let us test the slice() function with a string. The slice() function works the same as it worked with the list. See the code below.

string = 'slice object to slice Python'

sliced = slice(20)

print(string[sliced])

The slice() function should return the string up to 20 characters. See the output below:

Example 3:

Let us tweak the previous example a bit and provide the ‘start’ and ‘end’ positions and slice the list with the slice() function. Here is the code:

lst = ("1""a""2""b""3""c""4""d")

slc = slice(2,8)

print(lst[slc])

Remember that if you provide the two arguments to the slice() function, it will consider them as ‘start’ and ‘end’ arguments. Here, ‘2’ is the starting point and ‘8’ is the ending point. The slice() function will start slicing the list from the 2nd position and will stop at the 8th position, returning everything which is in between these two points. Let us check the output given below to see what the slice() function has returned.

Note that the slice() function started from ‘2’ and stopped at ‘d’ as ‘2’ is present at the 2nd position and ‘d’ is present at the 8th position.

Example 4:

As the slice() function takes 3 total arguments, let us write a program providing all three arguments to understand its workings. See the sample code below:

lst = ("1""a""2""b""3""c""4""d")

slc = slice(2,8,3)

print(lst[slc])

The rest of the lines of code are the same, we just tweaked the slice() function a bit by providing all three arguments as input. Here, ‘2’ represents the ‘start’ position, ‘8’ represents the ‘end’ position, and ‘3’ represents the ‘steps’ taken in the list while slicing. The ‘steps’ means that the slice() function will take the defined number of steps when it attempts to extract the next item from the list. As we provided ‘3’ for the ‘step’ argument, the slice() function will take the steps and return every third item from the list. All other items in between will be skipped and will not be returned as output. You can understand this by checking the output given below:

Now, compare the output of the 3rd and 4th examples and see the difference. The output from the third example returned 6 items from the list but the output from the fourth example returned only 2 items. This is because, in the third example, we did not provide the ‘step’ and slice() function that returned everything which was in between the ‘start’ and ‘end’ positions. However, when we provided the ‘steps’ arguments in the fourth example, the slice() function took the particular ‘steps’ each time it extracted an item from the list resulting in only 2 items being returned. This is very useful in only getting the desired items from the list and skipping all others which are not required.

Conclusion

In this article, we explored the slice() function in python programing language. The slice() function is used to get the sliced items from the list. It takes three arguments: start, stop, and steps, and returns a sliced object. It is used to slice any sequence in the python programming language including bytes, lists, ranges, tuples, and strings.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content