Tuple
Tuple
• Tuple is a collection of Python objects much like a list.
• The sequence of values stored in a tuple can be of any type, and they
are indexed by integers.
• Values of a tuple are syntactically separated by ‘commas’.
• Although it is not necessary, it is more common to define a tuple by
closing the sequence of values in parentheses. This helps in
understanding the Python tuples more easily.
Creating a Tuple
• In Python, tuples are created by placing a sequence of values separated
by ‘comma’ with or without the use of parentheses for grouping the
data sequence.
• Note: Creation of Python tuple without the use of parentheses is
known as Tuple Packing.
• Tuples can contain any number of elements and of any datatype
• Tuples can also be created with a single element, but it is a bit tricky.
Having one element in the parentheses is not sufficient, there must be
a trailing ‘comma’ to make it a tuple.
Accessing of Tuples
• Tuples are immutable, and usually, they contain a sequence of
heterogeneous elements that are accessed via unpacking or indexing
(or even by attribute in the case of named tuples).
• Lists are mutable, and their elements are usually accessed by iterating
over the list.
• In unpacking of tuple number of variables on the left-hand side
should be equal to a number of values in given tuple.
• tup1 = ('physics', 'chemistry', 1997, 2000)
• tup2 = (1, 2, 3, 4, 5, 6, 7 )
• print "tup1[0]: ", tup1[0]
• print "tup2[1:5]: ", tup2[1:5]
• Output:
• tup1[0]: physics
• tup2[1:5]: [2, 3, 4, 5]
Updating Tuples
• Tuples are immutable which means you cannot update or change the values
of tuple elements.
• You are able to take portions of existing tuples to create new tuples.
• tup1 = (12, 34.56)
• tup2 = ('abc', 'xyz')
• # Following action is not valid for tuples
• # tup1[0] = 101
• # So let's create a new tuple as follows
• tup3 = tup1 + tup2
• print tup3
Concatenation of Tuples
• Concatenation of tuple is the process of joining two or more Tuples.
Concatenation is done by the use of ‘+’ operator. Concatenation of
tuples is done always from the end of the original tuple. Other
arithmetic operations do not apply on Tuples.
• Note- Only the same datatypes can be combined with concatenation,
an error arises if a list and a tuple are combined.
• (1, 2, 3) + (4, 5, 6)
• (1, 2, 3, 4, 5, 6)
Deleting a Tuple
• Tuples are immutable and hence they do not allow deletion of a part
of it. The entire tuple gets deleted by the use of del() method.
• tup = ('physics', 'chemistry', 1997, 2000)
• print tup
• del tup
• Can’t delete tup[0]
Indexing and Slicing of Tuple
• Slicing of a Tuple is done to fetch a specific range or slice of sub-
elements from a Tuple.
• Slicing can also be done to lists and arrays. Indexing in a list results to
fetching a single element whereas Slicing allows to fetch a set of
elements.
Tuple Operations
• Repetition:The repetition operator enables the tuple elements to be
repeated multiple times.
• T1*2 = (1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
• Iteration:The for loop is used to iterate over the tuple elements.
• for i in T1:
• print(i)
Tuple Membership Test
We can test if an item exists in a tuple or not, using the keyword in.
my_tuple = ('a', 'p', 'p', 'l', 'e',)
# in operation Output
print('a' in my_tuple)
print('b' in my_tuple) True
False
# not in operation True
print('g' not in my_tuple)
Built-in-Method
• index( ) find in the tuple and returns the index of the given value where it’s
available
• count( ) returns the frequency of occurrence of a specified value
• all() Returns true if all element are true or if tuple is empty
• any() return true if any element of the tuple is true. if tuple is empty,
return false
• len() Returns length of the tuple or size of the tuple
• max() return maximum element of given tuple
• min() return minimum element of given tuple
• sum() Sums up the numbers in the tuple
• sorted() input elements in the tuple and return a new sorted list
• tuple() Convert an iterable to a tuple.
List vs. Tuple
SN List Tuple
1 The literal syntax of list is shown by the []. The literal syntax of the tuple is shown by the ().
2 The List is mutable. The tuple is immutable.
3 The List has the a variable length. The tuple has the fixed length.
4 The list provides more functionality than a The tuple provides less functionality than the
tuple. list.
5 The list is used in the scenario in which we The tuple is used in the cases where we need to
need to store the simple collections with no store the read-only collections i.e., the value of
constraints where the value of the items can be the items cannot be changed.
changed.
6 The lists are less memory efficient than a The tuples are more memory efficient because of
tuple. its immutability.