Python Tuple Slice Tutorial

In this section, we will learn how to slice a tuple object in Python.

What is Python Tuple Slice?

Python Tuple Slice is a way of accessing a range of elements of a tuple object!

For example, let’s say we want to take a sub-tuple with x number of elements from a tuple object, using Slice we can easily do that.

Python Tuple Slice Syntax:

tupleObject[start: stop: step]

`tupleObject`: this is the tuple object that we want to take a sub-tuple from it.

`start`: this is the start index where we want to take the elements from that position. Note that this value is optional and if ignored, the value 0 (meaning the first element of the tuple object) will be used instead.

`stop`: this is the index number where we want the sub-tuple to stop there. Note that this value is excluding! Meaning if the value is 10 for example, the element in the index number 9 will be included in the sub-tuple object but not the element in the index number 10. Also remember that this value is optional and if ignored, the value for the size of the target tuple object will be used instead.

`step`: this is the value that defines the steps for the elements of the tuple object to be added to the sub-tuple object. For example, if the value is 2, it means take the first element specified at the index `start`, then ignore one element at a time and take the next one until we reach to the `stop` index. Note that this value is optional and if ignored the value 1 will be used instead (means no skipping).

Example: using python Tuple slice

tup1 = ("Ellen","Jack", "Omid","Elon", "James","Richard","Jeremy", "Ian", "Kimberly")

sub_tuple = tup1[2:6]

print(sub_tuple)

Output:

('Omid', 'Elon', 'James', 'Richard')

Negative Indexing in Python

We can use negative numbers as the values for the `stop`, `start`, and `step` as well.

Negative index points to the last elements towards the beginning elements.

For example, if we use the value -1, it means the last element of a tuple object. Or if we use the value -2 it means the element before the last element of the target tuple object.

So if we put a slice like:

tupleObject[-4:-1]

That means, start from the fourth element from the end of the tuple object and then take every element until we reach to the index -1 (which is exclusive as mentioned before).

Example: python Tuple slice and negative index numbers

tup1 = ("Ellen","Jack", "Omid","Elon", "James","Richard","Jeremy", "Ian", "Kimberly")

sub_tuple = tup1[-5:-1]

print(sub_tuple)

Output:

('James', 'Richard', 'Jeremy', 'Ian')

Note that by default the direction of taking elements is from start of a tuple object towards its ending part. But we can change this direction to start from the end of a tuple object and move towards the beginning of that tuple.

To do this, we need to change the value of the `step` to a negative number like `-1` for example.

Note: when making the `step` value into a negative, then the value of the `start` index should be higher than the value we put for the `stop` index (because the direction is now reversed.

Example: slicing a tuple object using negative value for `step`

tup1 = ("Ellen","Jack", "Omid","Elon", "James","Richard","Jeremy", "Ian", "Kimberly")

sub_tuple = tup1[6:0:-1]

print(sub_tuple)

Output:

('Jeremy', 'Richard', 'James', 'Elon', 'Omid', 'Jack')

Python Tuple Slice: leaving out the first value

As mentioned at the beginning of this section, we can skip the first value `start` index. In a situation like this, the default value 0 will be used instead.

Example: Tuple slice without the first value

tup1 = ("Ellen","Jack", "Omid","Elon", "James","Richard","Jeremy", "Ian", "Kimberly")

sub_tuple = tup1[:8]

print(sub_tuple)

Output:

('Ellen', 'Jack', 'Omid', 'Elon', 'James', 'Richard', 'Jeremy', 'Ian')

Python Tuple Slice: leaving out the last value

We can also leave out the `stop` index when slicing a tuple object! In that case, the value for the size of the target tuple object will be used instead.

Example: Tuple slice without the last value

tup1 = ("Ellen","Jack", "Omid","Elon", "James","Richard","Jeremy", "Ian", "Kimberly")

sub_tuple = tup1[2:]

print(sub_tuple)

Output:

('Omid', 'Elon', 'James', 'Richard', 'Jeremy', 'Ian', 'Kimberly')

And finally, we can leave out the values for `stop` and `start` indexes. In that case, an exact replica of the target tuple object will be returned.

Example: leaving out the values of `start` and `stop` indexes

tup1 = ("Ellen","Jack", "Omid","Elon", "James","Richard","Jeremy", "Ian", "Kimberly")

sub_tuple = tup1[:]

print(sub_tuple)

Output:

('Ellen', 'Jack', 'Omid', 'Elon', 'James', 'Richard', 'Jeremy', 'Ian', 'Kimberly')

Leave a Reply