What is Tuple in Python
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.
Go through this Python Course in London to get a clear understanding of Python!
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
You can learn and master Python from Intellipaat’s Python course in Bangalore
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
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
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)
Interested in learning Python? Check out the Python Training in Sydney!
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
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. |
If you want to know why Python is the most preferred language for data science, you can go through this Python Data Science tutorial.
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))
The output will be: (5,10,15,20,25)
Further, check out our offers for Python training courses prepared by industry experts.
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 blog.
Further, check out our offers for Python training Courses and also refer to the trending Python coding interview questions prepared by the industry experts.