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]:

l=[10,20,30,40,50]
print(l[0:5:2])
Output:
[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:

Syntax:
l[start:stop:step]

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:

l=[10,20,30,40,50]
print(l[1:4])
print(l[4:1:-1])
Output:
[20, 30, 40]
[50, 40, 30]
l=[10,20,30,40,50]
print(l[-1:-6:-1])
print(l[::-1])
Output:
[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.

l1=[10,20,30]
l2=l[:]
t1=(10,20,30)
t2=t1[:]
s1 ="geeks"
s2=s1[:]
print(l1 in l2)
print(t1 in t2)
print(s1 in s2)

Output:
False
False
True

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