Tuples in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Video Tutorial

Tuples part 1

Overview

A tuple is one of the four inbuilt data types used to store collections in Python. Unlike other data types, the elements in tuples are ordered and immutable. They are used to store multiple items in a single variable and provides some built-in operation to work with them.

Introduction to Tuples in Python

Python has multiple data structure collections to offer like lists, tuples, sets, and dictionaries. But these tuples are pretty similar to lists. Lists being a commonly used data structure by the developers, they usually confuse how tuples are different to lists.

Let’s understand tuples in python deeply and see its application, use cases, and how they’re different from the commonly used data structure collection, Lists.

Well, Python tuples are a collection of elements of any data type just like lists, but tuples are immutable i.e. we cannot change the elements of the tuple or the tuple itself, once it's assigned whereas we can change the elements of the list as they are mutable.

Creating a Tuple in Python

Tuples can only be created when they are being assigned, hence placing all the elements inside the parenthesis, separated by a comma will create a tuple. Let’s take a closer look at the syntax:

Output

The parenthesis in the above syntax is optional and hence a tuple can also be created without writing the parenthesis. But remember it’s always a good practice to have the parenthesis, as it always increases the code readability and understanding.

The above snippet can also be written as:

Output

How to Define a Tuple in Python with a Single Element?

What if in the above snippet you write tempTuple = (‘apple’) OR tempTuple = ‘apple’ instead of tempTuple = (‘apple’, ‘mango’, ‘banana’) OR tempTuple = ‘apple’, ‘mango’, ‘banana’ ?

Well, python will consider it as a string in this case, hence a trailing ‘,’ is a mandate inorder for the python interpreter to interpret it as a tuple.

Output

Accessing Elements in a Tuple in Python and Indexing

Accessing elements in a tuple is no different then accessing elements in a list. As python follows 0 based indexing hence a tuple with n elements will have indices from 0 through n-1. An index in a tuple is accessed with the index operator [ ]. For example:

Let’s consider a basic tuple:

Nested Python Tuple Accessibility

Accessing via Negative Indices

Python allows you to access elements of a collection via negative indices. When accessing using a negative index, -1 depicts the last element, and -n depicts the first index where n is the length of the index.

Consider the following mapping of the positive index with negative index:

accessing tuples via negative indices

Updating Tuples in Python:

Adding a new element or deleting one is not really an option when dealing with tuples in Python, as they are immutable. Even the elements of the tuple cannot be updated until and unless the element is mutable for example a list.
Let’s take an example

Output

Tuples in Python can definitely be reassigned, which is very different from updating a tuple. Reassigning a tuple is redefining the tuple all over again.

Just like strings, we can also concat two or more tuples to form a new tuple using ‘+’ operation or apply repetition on a tuple using ‘*’ operator, just the result here is a Python tuple and not a string.

Output

Deleting Tuples in Python

As discussed, python tuples being immutable cannot be updated. Hence once some values are assigned to a tuple, it cannot be deleted. You can delete a tuple as a whole, but deleting a specific value/element in a tuple is not possible. Let’s look at the example:

Output

In-built Functions for Tuple:

Python has the following inbuilt functions to offer for tuples: slicing in python tuples

Slicing in Tuples

Slicing in tuples works the same as it works for a String slicing or any other sequence of elements. Slice is an operator that allows you to fetch a sub collection (in this case a sub tuple) from a collection by slicing it from a start index and a stop index.

Slice syntax:

tuple[start : stop : step]

  • start: is the starting index of the string, on which slicing operation has to be performed. It determines from where slicing of the string will ‘begin’.
  • stop: is the stopping index of the slicing, ‘until’ which slicing operation has to be performed i.e stop index is excluded while generating the sub-tuple.
  • step: It is an optional argument that defines the steps when iterating the list i.e. it allows us to skip over elements.

Consider the above figure when understanding the following code snippet

Output

Basic Python Tuple Operations

Just like ‘+’ and ‘*’ operations, tuple also respond to other sequential operations like length, membership and for loop. Consider the following example:

Output

Advantages and Disadvantages of Tuple in Python

Advantages

  • Tuples being immutable, turn out to be a write-protected collection. Tuples can be of advantage when we want to store some secure read-only data that we cannot afford to be changed throughout our code.
  • Tuples can store data of multiple data types, which makes them a heterogeneous collection.
  • Tuple being a read-only collection, has a faster iteration. (As they are stored in a single block of memory, and don’t have extra space for storing objects, they have a constant set of values)

Disadvantages

  • Tuple’s being write-protected, is an advantage but also a disadvantage as it cannot be used when we want to add or delete a specific element. Hence has a limited use case.
  • Syntactically less readable as tuples can be created by either adding parentheses or by not providing them in case we have more than one element. But not using parentheses in case of one element, will not create a tuple, and hence a trailing comma in such case is required. This can make code readability a bit complex for some.
  • As tuple is a class, it's stored on the heap and is overhead on the garbage collector.

Tuple’s advantages and disadvantages are nothing but its use cases i.e. tuple serves some use cases hence one should know when to use tuple, in order to use it for their advantage. Tuples when used where a list or a dictionary or a set would’ve been used, will turn out to be a disadvantage.

Conclusion:

  • Tuples are one of the data structures that python has to offer and are immutable, ordered, have integer-based indexing, and allow duplicate data to be stored.
  • Tuples can be created both using parentheses and comma-separated elements AND without parentheses and comma-separated elements. If not using parenthesis, a trailing comma should be added for Python to interpret as a tuple.
  • Deleting an element from a tuple is not possible. But deleting a tuple is.
  • Overall, tuples are of advantage when used for the use case it is designed for. Similarly are of disadvantage, if used where a list, set, or dictionary should’ve been used. Hence one should analyze what their use cases are and decide the corresponding data structure or collection to be used. Similarly, this is valid for all data structures.