lettura simple

Slicing in Python

Slicing is one of the most useful and powerful functions in the Python language for processing data sequences. In this lesson, I will explain what slicing is, how it works, and how to use it in Python.

So, what is slicing? Slicing is a Python technique that allows you to select and extract a part of a data sequence (e.g., strings, lists, tuples, etc.) between two arbitrary indices. For example, you can use slicing to extract a substring from a larger string, a sublist from a list, and so on.

How does slicing work?

To use slicing in Python, you need to use this syntax:

[start:end:step]

The syntax is made up of three parameters:

  • start is the starting position in the data index that you want to extract. It is included in the selection.
  • end is the ending position of the data to be extracted. It is excluded from the selection.
  • step is the gap between each element. By default, it is equal to 1.

Note that if you do not specify any of these parameters, the default values are used: start is set to position 0 in the index, end is set to the last position in the index, and step is set to 1.

Let me give you a practical example.

First, create a string variable:

>>>string = "This is an example string"

Then type the command string[11:18]

>>> string[11:18]

This command extracts the substring that starts at index position 11 of the string and ends at position 18 (excluded).

an example of slicing

Note that in the Python language, the first character of a string occupies position 0 in the index. The second character occupies position 1, and so on. The same rule applies to all other data sequences in Python (e.g., lists, tuples, etc.).

In this case, Python extracts the substring "example".

example

Let me give you another example.

Create a list containing a sequence of numbers from 1 to 10:

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

Then type list[0:3] to select the first three numbers of the list:

>>> list[0:3]

In this case, the start is 0, and the end is 3. You did not specify the end parameter, so it defaults to 1.

The command returns the first three elements of the list:

[1, 2, 3]

Now type list[2:5]

>>>list[2:5]

This command selects the portion of data from the element at position 2 to the element at position 5 (excluded).

[3, 4, 5]

In this way, you can extract any intermediate sublist.

Explanation. The element '3' is at position 2 in the index of the list, i.e. list(2)=3. The element '5' is at position 4 in the index, i.e. list(4)=5.
a sublist

If you do not specify a slicing parameter, Python uses the default value.

For example, type list[4:]

>>> list[4:]

In this case, Python extracts a sublist from position 4 in the index to the last element of the list.

example of sublist

The result is the following sub-list

[5, 6, 7, 8, 9, 10]

You can also change the step of the selection. For example, type list[0:5:2]

>>> lista[0:5:2]

Python extracts a sublist from element 0 to element 5, skipping the intermediate elements.

a sublist

In this case, the extracted sub-list is:

 

[1, 3, 5]

If you specify a negative number, Python considers the positions starting from the last element.

For example, to select the last three elements of the list, type list[-3:]

>>> list[-3:]

In this case, Python extracts the elements starting from the third-to-last position.

Python extracts the last elements of list

Python extracts these elements from the list.

[8, 9, 10]

In Python, if you omit both the start and end indices in slicing, the language conveniently returns a full copy of the sequence.

For instance, try typing list[:]

>>> list[:]

This command yields the entire list as a result.

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

To reverse a list, all you need to do is use the entire sequence with a step value of -1.

>>> list[::-1]

The outcome is a sequence arranged from the last element to the first.

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

An interesting feature of slicing is its tolerance for indices that fall outside the range. For example, if you enter

>>> list[-100:100]

You'll still receive the sequence arranged from the last element to the first as a result.

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

These are just a handful of the numerous practical slicing techniques.

Slicing proves to be an exceptionally versatile and powerful tool in data sequence manipulation, applicable to lists, strings, and tuples alike.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin