How to Use the Python slice() Function


When writing Python programs, you will often make use of a range of items (such as numbers, letters, etc.). For example, you might use the numbers 1 through 10 in a list, and throughout your application, you might need to refer to different sections of that list. In one instance, you might only want to refer to the first two characters. In another section, you might want to reference the range from indexes 2 to 6.

Here’s an example of a list:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

With Python, indexing is the processing of accessing elements in a sequence. These elements can be a single number, a string of numbers, a single word, a string of words, or a combination of different types. Each number (word, etc.) is an index, so 1 is index 0, and 10 is index 9 (because Python, like most programming languages, starts counting at 0 and not 1).

Indexing is an important concept because it is so often used.

To index sections of a sequence, Python uses the slice() function. With this function, you can specify where to start the slicing and where to end it. So, in our example above, we can tell Python to start the slice at index 2 (which would be 3) and end at index 7 (which would be 8). If we do this, our slice would be:

3, 4, 5, 6, 7, 8

Or, we could have a list of colors, like:

[red, blue, green, yellow, purple, pink, black, orange]

We could slice that sequence from 1-4, which would be:

blue, green, yellow, purple

The syntax for a slice looks like this:

Ah, we haven’t talked about the step yet.

By default, the slice() function steps by 1. So our above slicing works out. But you can specify the step you need. For example, if you step by 2 in our above list, the results would be:

blue, yellow

But why does it only include two elements? Remember, our slice went from 1-4 and steps by 2. Since yellow is our second element (which is index 4), the next jump (by a step of 2) would be 5, which is outside of our slice range, so it stops at 3 (yellow). If we started our slice at 0 and went to 4 (stepping by 2), the results would be:

red, green, purple

Starting to make sense now?

Okay, let’s see a slice in action. We’ll first create a slice example with numbers. Since slice() is a part of the built-in Python functions, we don’t have to call any outside libraries, so we can immediately start by defining my_list like so:

Next, we’re going to use the slice() function to define x as a slice object that goes from 2 to 7 with:

Finally, we use the print() function to print out our slice with:

What we’ve done above is use our defined slice variable (x) with our defined list (my_list). Create the new script with the command:

Our full script looks like this (I’ll add documentation for demonstration):

Save and close the file.

Run the script with the command:

The output will be:

[3, 4, 5, 6, 7]

Let’s create a similar script, that’s a bit more complicated. Create this with:

Add the following into the file:

Save and close the file.

Run the new script with:

The output should be the same as when you ran the slice.py script (so [3, 4, 5, 6, 7]).

Now, let’s use the step function in our first script. We’ll create the slice, stepping by 2. Go back to the slice.py script and change:

to:

Save and close the file. The new script should look like this:

Run the script with:

The output should be:

[3, 5, 7]

And that, my Python-loving friends, is how you use the slice() function.

Group Created with Sketch.



Source link

Leave a Comment