Slicing in Python (Tuples, Strings, Lists)

In the previous article we explored the conversions of Binary to Decimal. Now we will explore the concept of slicing, slicing in Python is the process of extracting a specific sub portion of an iterable data structure, according to the user-specified criteria. Very often, it is necessary to get not one certain element, but a certain set of them bounded by certain simple rules – for example, the first 5 or the last three, or every second element – in such tasks, instead of iterating over in a loop, it is much more convenient to use the so-called slice (slice, slicing). It should be remembered that by taking an element by index or slice, we do not change the original collection, we just copied part of it for further use (for example, adding to another collection, printing, some calculations).┬áSince the collection itself does not change, this applies to both mutable (list) and immutable (string, tuple) sequences.

When it comes to slicing in Python we can slice a tuple, a string or a list. Let us look at an illustrative example. We have a list with 5 items, L=[10,20,30,40,50]:

[10, 30, 50]

In the above code, we extract the elements 10, 30 and 50 specifically through the use of slicing. The index begins at 0 and increases as we move further. The syntax of the slicing statement is as follows:


The first parameter is taken as the position from where the slice begins, the second parameter is the index position where it should stop. The last parameter, that is the ‘step’ parameter instructs by how how much the list position should increase or decrease by at each successive stage. List slicing returns a list, tuple slicing returns a tuple and string slicing returns a string. However all these data structures handle these parameters in the same manner. If the start position is not mentioned, it is taken as 0. The default value of stop is the same as the length of the list. When step is not mentioned the default value of 1 is considered.

The parameter values can also be negative:

[20, 30, 40]
[50, 40, 30]
[50, 40, 30, 20, 10]
[50, 40, 30, 20, 10]

The second print statement can be considered as a shortcut for the first one as the result in the same output. Let us see another program to demonstrate an interesting fact. We have a list l1, we make a copy of this list as the value of l2 through the use of slicing. Similarly, we make duplicates of tuple t1 and string s1.

s1 ="geeks"
print(l1 in l2)
print(t1 in t2)
print(s1 in s2)


The difference between slicing of list, and tuple and string is instead of list you always get a different list as output. They are considered as different objects, but in the case of the tuple and the string they are seen by the program as the same objects. Rather than really creating a new tuple or string, Python references the same memory address unlike how it handles lists.

Thus, we have explored how Python handles the slicing of different data structures. In the next article we will explore how to obtain ‘smaller elements’ from lists.

Leave a Comment