Python slicing usage example tutorial

  • 2020-04-02 14:06:29
  • OfStack

This article illustrates the usage of slice operation in Python in the form of an example and shares it with you for your reference. The details are as follows:

Taking a partial element of a list or tuple is a very common operation. For example, a list is as follows:


>>> L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']

So if I take the first three elements, what should I do?

Here's the dumb way:


>>> [L[0], L[1], L[2]]
['Michael', 'Sarah', 'Tracy']

And the reason it's stupid is because if you expand it, you don't have to take the first N elements.

Take the first N elements, that is, the elements whose index is 0-(N-1), you can use the loop:


>>> r = []
>>> n = 3
>>> for i in range(n):
...   r.append(L[i])
... 
>>> r
['Michael', 'Sarah', 'Tracy']

Looping is cumbersome for such operations, which often take a specified index range, so Python provides the Slice operator, which greatly simplifies the operation.

Corresponding to the above problem, take the first three elements and slice them with one line of code:


>>> L[0:3]
['Michael', 'Sarah', 'Tracy']

L[0:3] means that you start at index 0 and end at index 3, but you don't include index 3. The index is 0,1,2, which is exactly 3 elements.

If the first index is 0, you can also omit:


>>> L[:3]
['Michael', 'Sarah', 'Tracy']

You can also start with index 1 and pull out two elements:


>>> L[1:3]
['Sarah', 'Tracy']

Similarly, since Python supports L[-1] to take the reciprocal of the first element, it also supports the reciprocal slicing. Try this:


>>> L[-2:]
['Bob', 'Jack']
>>> L[-2:-1]
['Bob']

Remember that the index of the last element in the reciprocal is minus 1.

The slicing operation is very useful. Let's create a sequence of Numbers from 0 to 99:


>>> L = range(100)
>>> L
[0, 1, 2, 3, ..., 99]

You can easily take out a certain sequence by slicing. For example, the first 10 Numbers:


>>> L[:10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

The last 10 Numbers:


>>> L[-10:]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

The first 11-20 Numbers:


>>> L[10:20]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

For the first 10 terms, take one for every two:


>>> L[:10:2]
[0, 2, 4, 6, 8]

All Numbers, one for every 5:


>>> L[::5]
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]

You can even copy a list by writing nothing but [:] :


>>> L[:]
[0, 1, 2, 3, ..., 99]

A tuple is also a list, the only difference being that a tuple is immutable. So a tuple can also be sliced, but the result is still a tuple:


>>> (0, 1, 2, 3, 4, 5)[:3]
(0, 1, 2)

The string 'XXX' or Unicode string u' XXX 'can also be considered a list, with each element being a character. Therefore, strings can also be sliced, but the result is still a string:


>>> 'ABCDEFG'[:3]
'ABC'
>>> 'ABCDEFG'[::2]
'ACEG'

In many programming languages, a variety of interceptors are provided for strings, essentially slicing and dicing strings. Python has no interceptor function for strings, and it's as simple as slicing.

Conclusion:

With the slicing operation, there are many places where the loop is no longer needed. Python slicing is so flexible that you can loop through many lines of code in one line.

Hopefully, the examples described in this article will help you get a better grasp of Python programming.


Related articles: