Python: Slice Notation on Tuple

Introduction

The term slicing in programming usually refers to obtaining a substring, sub-tuple, or sublist from a string, tuple, or list respectively.

Python offers an array of straightforward ways to slice not only these three but any iterable. An iterable is, as the name suggests, any object that can be iterated over.

In this article, we'll go over everything you need to know about Slicing Tuples in Python.

Slicing a Tuple in Python

There are a couple of ways to slice a tuple, most common of which is by using the : operator with the following syntax:

tuple[start:end]
tuple[start:end:step]

The start parameter represents the starting index, end is the ending index, and step is the number of items that are "stepped" over.

If step isn't explicitly given, the default value is 1.

Let's go ahead and slice a tuple:

a_tuple = ('You', 'can’t', 'stop', 'the', 'change,', 'any', 'more', 'than', 'you', 'can', 'stop', 'the', 'suns', 'from', 'setting.')
subtuple = a_tuple[1:5]
print(subtuple)

The resulting tuple will be:

('can’t', 'stop', 'the', 'change,')

Finding the First and the Last n Elements of a Tuple

To find the first and last n elements of a tuple, we'll slice from the first element to n and then from -n to the end of the tuple.

-n will start counting from the end of the tuple, backwards, giving us the last n elements:

n = 2
a_tuple = ('I', 'find', 'your', 'lack', 'of', 'faith', 'disturbing.')

first_n_tuple = a_tuple[:n]
print(first_n_tuple)
last_n_tuple = a_tuple[-n:]
print(last_n_tuple)

The resulting subtuples are:

('I', 'find')
('faith', 'disturbing.')

Reverse Tuple with Slice Notation

If we set the step parameter to -1, it'll start stepping backwards. If we include the whole, or even a part of the tuple, and just step backwards, it'll effectively reverse the tuple's contents:

a_tuple = ('Fear', 'is', 'the', 'path', 'to', 'the', 'dark', 'side.', 'Fear', 'leads', 'to', 'anger;', 'anger', 'leads', 'to', 'hate;', 'hate', 'leads', 'to', 'suffering.', 'I', 'sense', 'much', 'fear', 'in', 'you.', 7)
reverse_tuple = a_tuple[::-1]
print(reverse_tuple)

The reversed tuple will be:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

(7, 'you.', 'in', 'fear', 'much', 'sense', 'I', 'suffering.', 'to', 'leads', 'hate', 'hate;', 'to', 'leads', 'anger', 'anger;', 'to', 'leads', 'Fear', 'side.', 'dark', 'the', 'to', 'path', 'the', 'is', 'Fear')

Finding Every n-th Element of a Tuple

To find every n-th element of a tuple, we'll jump over all non-n elements by setting the step to be n:

n = 5
a_tuple = ('The', 'dark', 'side', 'of', 'the', 'Force', 'is', 'a', 'pathway', 'to', 'many', 'abilities', 'some', 'consider', 'to', 'be', 'unnatural.')
replaced_tuple = a_tuple[::n]
print(replaced_tuple)

This will result in a tuple:

('The', 'Force', 'many', 'be')

Conclusion

Slicing any sequence in Python is easy, simple, and intuitive. Negative indexing offers an easy way to acquire the first or last few elements of a sequence, or reverse its order.

In this article, we've covered how to apply the Slice Notation on Tuples in Python.

Last Updated: September 20th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Kristina PopovicAuthor

CS student with a passion for juggling and math.

Project

Building Your First Convolutional Neural Network With Keras

# python# artificial intelligence# machine learning# tensorflow

Most resources start with pristine datasets, start at importing and finish at validation. There's much more to know. Why was a class predicted? Where was...

David Landup
David Landup
Details
Course

Data Visualization in Python with Matplotlib and Pandas

# python# pandas# matplotlib

Data Visualization in Python with Matplotlib and Pandas is a course designed to take absolute beginners to Pandas and Matplotlib, with basic Python knowledge, and...

David Landup
David Landup
Details

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms