Tuples - An Immutable Derived Datatype
Some documents that have unique numbers (passport, driving license, voter id, and the pixels of an image) that cannot/should not be changed. Such elements are stored in a set that satisfies all the requirements known as a tuple.
In this article, you will learn the different operations where we use a tuple.
What is a tuple in Python?
So, it's basically a sequence of different elements that we access via unpacking or indexing.
More clearly, a collection of objects in a round bracket in python and separated by a comma is known as a tuple. Doesn't it seem like 'list?'
Then what is the difference between list and tuple?
A list and tuple are the same in some ways when it's related to indexing, nested objects, and repetition. Even if we don't use any brackets, it becomes a tuple. However, a tuple is immutable and lists are mutable.
#Creating an empty tuple
empty_tuple = ()
#one way of creating
tup = ('Data', 'Science')
#another way of creating
tup = 'Data', 'Science'
Creating Immutable tuple
An example shown below is that the item assignment is not possible.
| #testing that tuples are immutable
tuple_1 = (5, 6, 8, 12)
tuple_1 = 'Numbers'
TypeError Traceback(most recent call last)
Input In , in <cell line: 4>()
1# testing that tuples are immutable
3 tuple_1 = (5, 6, 8, 12)
--->4 tuple_1 = 'Numbers'
TypeError: 'tuple' object does not support item assignment |
How to perform basic operations in a tuple?
In a tuple, there are several ways to do the basic operations. Let's take a look at each one individually with examples.
Since the tuple is immutable, you cannot change any value in the tuple element. But, you can combine two tuples as a single tuple which is called _ tuple concatenation _.
#creating two tuple
tuple1 = 5, 6, 7, 8
tuple2 = ('data', 'science', 'python', 'class')
#concatination of tuple
print(tuple1 + tuple2)
(5, 6, 7, 8, 'data', 'science', 'python', 'class') |
If you need any value in a tuple to be repeated, you just need to multiply it by several times.
| # Creating tuple with repetition
tuple_repeat = ('How are you? ')*5
How are you? How are you? How are you? How are you? How are you? |
A slicing is retrieving a particular portion for some operation while the initial data remains unaffected. The operation of slicing in python considers three parameters of which two are depending on the operational requirement.
| # Slicing of tuple
tuple = (10, 30, 50, 87, 63)
print(tuple[: : -1])
(30, 50, 87, 63)
(63, 87, 50, 30, 10)
(50, 87, 63)
Finding the length of the Tuple
You can find out the length of values inside a tuple by using the 'len' option.
| # Finding the length of tuple
tuple_length = (5, 6, 7, 8, 'data', 'science', 'python', 'class')
Basic functions to use while executing a code in the tuple
- len() – you can find the length in a tuple
- min() – you can find the min value in a tuple
- max()– you can find the max value in a tuple
- count() – you can find the number of occurrences in a tuple
- index() – you can find the index value of a tuple
Note : Count and index are not functions, they are the methods
#length, minimum, maximum, count and index of tuple
tuple = (10, 30, 50, 87, 63, 10, 87)
print('Length of the tuple: ', len(tuple))
print('Minimum of the tuple: ', min(tuple))
print('Maximum of the tuple: ', max(tuple))
print('Count of 87: ', tuple.count(87))
index = tuple.index(63)
print('Index of 63: ', index)
Length of the tuple: 7
Minimum of the tuple: 10
Maximum of the tuple: 87
Count of 87: 2
Index of 63: 4 |
Sorting of tuple
By using the sort() method you can sort a tuple. When sorting by this method the real content of the tuple is changed and the in-place method of the sort is performed.
#Sorting a list of tuples by the second item using sort()
#Function for sorting the list by the second item of tuple |
#reverse = None(Sorting in ascending order)
tup_1.sort(key = lambda a: a)
tup_1 = [('Avni', 10), ('Akash', 5), ('Avinash', 20), ('Arvind', 15)]
[('Akash', 5), ('Avni', 10), ('Arvind', 15), ('Avinash', 20)] |
Nesting of tuples
We use the '+' operator when we need to concatenate tuples to nested tuples. Accessing the index elements and trying to make any changes will give an error. We use ''+' for both nesting and concatenating operations.
| first_tuple_1 = (5, 8, 4, 4, 7, 1),
second_tuple_2 = (9, 5, 3, 2, 1, 8),
print("The first tuple consists: ")
print("The second tuple consists: ")
joint_result = first_tuple_1 + second_tuple_2
print("The tuple after concatenating: ")
The first tuple consists:
((5, 8, 4, 4, 7, 1),)
The Second tuple consists:
((9, 5, 3, 2, 1, 8),)
The tuple after concatenating:
((5, 8, 4, 4, 7, 1), (9, 5, 3, 2, 1, 8)) |
Let us summarize in simple words, a tuple is a set where you cannot make any changes. In other words, it is immutable. With the above explanations and examples, you would have got a better understanding and ease to work on tuples. If you are looking for a best-in-class resource to learn python or understand each concept in detail for data science applications, you can check out the data science course with special python-support with top-class trainers. In case you are just exploring python as a beginner, follow us on Facebook, Youtube, Linkedin, Twitter.