Python Tuples

Tuples are a type of sequences like lists. The important thing to remember about tuples is that they are immutable.

Tuples are created using small brackets (). Just put any values separated by commas.

Sample Output

Creating Python Tuples
Creating Python Tuples


Tuples are immutable

Once you create a tuple, you cannot change parts of it. You can overwrite it completely if you want although.

This is the reason why tuples are faster than lists. Because, python doesn’t have to maintain the change log metadata.

Sample Output

Python Tuples are immutable
Python Tuples are immutable


When to use lists and when to use tuples in python?

When you need to store values dynamically, like running a loop and storing the results of each iteration, then use Lists. Because you can change a list dynamically.

When you need to iterate/reference values from a “fixed” set of values, then use tuples. e.g passing a list of hyper hyperparameters for grid search while parameter tuning in machine learning.


A tuple can contain a tuple!

Tuples are complex data structures, just like lists. Hence it can contain any value as one of the element. A number, string, list, tuple, dictionary etc.

Bottomline: A tuple can contain any python variable as an element.

In the below example, the complex tuple contains tuples as first two elements, a string as the third element and a list as the last element.

Now the tuples and list inside can also contain lists or tuples inside them! Creating inception of data structures! 🙂

Sample Output

A python tuple can contain a tuple inside it
A python tuple can contain a tuple inside it


Access elements from the tuple inside a tuple!

If you need to access the elements of the tuple inside the tuple or the list inside the tuple, then you need to use layer wise indexing. one set of square bracket [ ] for one layer.

For example to access the value ‘b’ from the complex tuple defined above, we need to first reach to the tuple present at second position using the index [1], then reach the value ‘b’ by providing the index for that tuple which is [5]. Hence there will be two set of brackets and indexes used, one for each layer. Always remember, python index begins from zero!

Sample Output

Accessing values in a tuple inside a tuple
Accessing values in a tuple inside a tuple


Commonly used tuple commands

Once you create any variable in python, it inherits functions from its parent class. Same happens with a tuple as well. You can explore the functions associated with a tuple variable by using dot(.) and tab button.

Tuples cannot be altered dynamically, hence the number of associated functions are only two!

Associated functions with a python tuple variable
Associated functions with a python tuple variable

Sample Output

Common tuple commands
Common tuple commands

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!

Leave a Reply!

Your email address will not be published. Required fields are marked *