Latest Code Tutorials

Python Tuple: The Complete Detailed Guide

In Python programming language, the tuple is similar to a list with the difference is that we cannot change the items of a tuple once it is assigned whereas, in the list, items can be modified. The Tuple is a collection of Python objects separated by commas.

Python Tuple

A tuple is a collection that is ordered and unchangeable. Python tuples are written with round brackets. Since tuples are immutable, iterating through a tuple is faster than a list. So there is a slight performance boost.

If you have data that doesn’t change in time then, implementing it as a tuple will guarantee that it remains write-protected.

deserts = ('oreo', 'cupcake', 'nougat', 'jellybean', 'pie')

Above is the simple example of Tuple in Python language.

Creating a Tuple

A tuple is formed by placing all the elements inside the parentheses (), separated by a comma. The parentheses are optional but are a good practice to write it. 

A tuple can have any number of items, and they may be of different types. Let us see the following example of different types of items in one tuple.


deserts = ('oreo', 1, True, ['Android', 'iOS'], 'pie')

In the above example, we have taken string, integer, list datatype to create a tuple and it is a valid tuple.

Let us write a tuple without parentheses.

deserts = 'oreo', 1, True, ['Android', 'iOS'], 'pie'

The above code is also valid but, it does not best practice to write that is why we add the parentheses around the items.

Access Tuple Items

You can access tuple items by referring to the index number, inside square brackets. Let us see the following example.


deserts = ('oreo', 1, True, ['Android', 'iOS'], 'pie')

See the following output.

Python Tuple Example Tutorial | Complete Introduction On Python Tuples

Loop Through a Tuple

We can iterate For loop through a tuple. Let us see the below example.


deserts = ('oreo', 'nougat', 'jellybean', 'pie')
for item in deserts:

The output is the following.

Complete Introduction On Python Tuples

Try To Change Tuple Values

Let us change the values in a tuple and see the result.


deserts = ('oreo', 'nougat', 'jellybean', 'pie')
deserts[1] = 'kitkat'

I am using Visual Studio Code, and I have installed a Python extension called pylint. That is why my editor throws me an error saying that ‘deserts’ does not support item assignment. That means we can not modify it. But still, let us run the file and see the output in the console.

Try To Change Tuple Values in Python

Tuple Length

If we want to determine how many items a tuple has, use the len() method.


deserts = ('oreo', 'nougat', 'jellybean', 'pie')

Tuple Length in Python

Remove Items in Tuple

Again, the items inside the tuple are unchangeable. But, we can delete the entire tuple. See the below example.


deserts = ('oreo', 'nougat', 'jellybean', 'pie')
del deserts

We have used the del keyword to delete the entire tuple. In the above code’s output, we will get an error because the tuple is already removed. So there is nothing to print because the compiler does not find any deserts tuple. So it throws an error.

Remove Items in Tuple

The tuple() Constructor

We can also use the tuple() constructor to make a tuple.


icecreams = tuple(('vanila', 'american nuts', 'soft serve', 'choc ice'))

The output will be the same.

The tuple() Constructor in Python

Basic Tuples Operations

Tuples respond to the + and * operators much like strings in python; they mean concatenation and repetition here too, except that a result is a new tuple, not a string.

Python Expression Results Description
len((1, 2, 3, 4, 5, 6)) 6 Length
(11, 21, 31) + (41, 51, 61) (11, 21, 31, 41, 51, 61) Concatenation
(‘App’,) * 4 (‘App’, ‘App’, ‘App’, ‘App’) Repetition
21 in (11, 21, 31) True Membership
for z in (11, 21, 31): print(z), 11 21 31 Iteration

Nesting of Tuples

Let us see the following example of nested tuples.


deserts = ('oreo', 'nougat', 'jellybean', 'pie')

icecreams = tuple(('vanila', 'american nuts', 'soft serve', 'choc ice'))

nestedTuples = (deserts, icecreams)

Nesting of Tuples in Python

Finally, Python Tuple example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.