Tuples in Python

Tutorial Playlist

Tuple data type in Python is a collection of various immutable Python objects separated by commas. Tuples are much similar to Python Lists, but they are syntactically different, i.e., in lists, we use square brackets while in tuples we use parentheses. In this module, we will learn all about the tuple data type in order to get started with it.
Following is the list of all topics that we will cover in this module.

So, without further delay, let’s get started.

Advantages of Tuples in Python over Lists

The main difference between Python tuples and lists is that the elements of a tuple cannot be changed once they are assigned; whereas, the elements of a list can be changed.

As tuples and lists are quite similar to each other, they are often used in similar kinds of situations. Although, a tuple in Python has a bunch of advantages over lists. Following are some of the main advantages:

  • Iteration in a tuple is faster as compared to lists since tuples in Python are immutable.
  • Tuples are generally used for different Python Data Types; whereas, lists are used for similar data types.
  • Whenever we need to make sure that the data remains unchanged and write protected, Python tuple is the best option.
Learn Python the Right Way
Structured Learning Path to Python Programming for Beginners and Experts
quiz-icon

Creating a Tuple in Python

A Python tuple is created using parentheses around the elements in the tuple. Although using parentheses is only optional, it is considered a good practice to use them.

Elements in the tuple can be of different data types or of the same data type. A tuple in Python can have any number of elements.

Following is the code block that shows how to create a tuple:

tup1 = (‘Intellipaat’, ‘ Python’, ‘tutorial’)
tup2 = 1,2,3,4
print (tup1)
print (tup2)

Output:
(‘Intellipaat’, ‘ Python’, ‘tutorial’)
(1,2,3,4)

Tuple length in Python

To evaluate the length of a tuple of the number of items it has, you can use the len() function 

tuple1 = (“python”,“java”,“c”)
print(len(tuple1))

The output will be 3

Accessing Python Tuple Elements

We can use three different ways of accessing elements in a tuple, that is, Indexing, reverse indexing, and using the slice operator.

Get 100% Hike!

Master Most in Demand Skills Now!

Indexing of Tuples in Python

To access an element of a tuple, we simply use the index of that element. We use square brackets around that index number as shown in the example below:

tup1 = (‘Intellipaat’, ‘Python’, ‘tutorial’)
print (tup1[0])
Output:
Intellipaat

Reverse Indexing of Tuples in Python

Much similar to regular indexing, here, we use the index inside the square brackets to access the elements, with only one difference, that is, we use the index in a reverse manner. Meaning, the indexing of the elements would start from the last element. Here, we use indexes as −1, −2, −3, and so on, where −1 represents the last element.

Following code, block is an example to access elements using reverse indexing.

tup1= (‘Intellipaat’, ‘Python’, ‘tutorial’)
print (tup1[-1])

Output:
tutorial
Python Developer Certification
Gain In-Depth Knowledge of Python for Development, Data, and AI
quiz-icon

Slicing Operator of Tuples in Python

Using the slicing operator to access elements is nothing new, as we have seen this in previous modules as well. As the name suggests, we will slice, that is, extract some elements from the tuple and display them. To do this, we use a colon between the index from where we want to start slicing and the index till where we want to perform it.

The following code block is an example to show how to access elements using the slicing operator.

tup3 = (1,2,3,4,5,6)
tup3[1:]
tup3[2:4]

Output:
(2, 3, 4, 5, 6)
(3, 4)

Performing Operations in Tuples in Python

Following is the list of some of the most frequently used operations in a Python tuple along with their descriptions and examples.

Deleting Python Tuple Elements

Since a tuple in Python is an immutable data type in Python, deleting particular elements in a tuple is not possible. But, the whole tuple can be deleted using the del keyword as shown in the following example:

tup1 = (‘Intellipaat’, ‘Python’, ‘tutorial’)
print (tup1)
del tup1
print (tup1)

Output:
(‘Intellipaat’, ‘Python’, ‘tutorial’)
Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘tup1’ is not defined
Python for Data Science and Automation
Automate Tasks and Analyze Data with Python’s Powerful Libraries
quiz-icon

Modifying Elements in a Python Tuple

Again, since a tuple is immutable, it is impossible to change or modify the value of a particular element. Although, we can take some portion of an existing tuple and create a new tuple using the concatenating operator, as shown in the example below:

tup1 = (‘Intellipaat’, ‘Python’, ‘tutorial’)
tup2 = (1,2,3)
tup3 = tup1 + tup2
print (tup3)

Output:
(‘Intellipaat’, ‘Python’, ‘tutorial’,1,2,3)

Difference between list and tuple in python

The table below explains the differences between lists and tuples in Python.

Lists Tuples
Lists are mutable. Tuples are immutable.
Iterations are time-consuming. Iterations are comparatively faster.
To perform operations like insert,
delete etc., lists are better.
Tuples are better to access elements
Lists have many built-in methods Tuples have fewer built-in methods
Lists consume more memory. Tuples consume less memory than lists.

Python List of Tuples

Here is the python code for creating a list of tuples in python.

list1 = [1,3,8,9]
res = [(val, pow(val,3)) for val in list1]
print(res)

The output will be:
[(1, 1), (2, 8), (5, 125), (6, 216)]

List to Tuple in Python

To typecast to tuple, you can simply use tuple(list_name)

def convert(a):
  return tuple(a)
a = [5,10,15,20,25]
print(convert(a))
Output
(5,10,15,20,25)

With this, we come to the end of this module of Python Tutorial. Now, if you want to know why Python is the most preferred language for data science, you can go through this Python for Data Science course by Intellipaat.
Further, check out our offers for Python training Courses and also refer to the trending Python coding interview questions prepared by industry experts.

Our Python Courses Duration and Fees

Program Name
Start Date
Fees
Cohort starts on 11th Jan 2025
₹20,007

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.