Tuple in Python PYTHON by Ravinder Nath Rajotiya - March 12, 2022March 20, 20220 Share on Facebook Share Send email Mail Print Print Table of Contents Toggle Tuplesa). Declaring and accessing tuple using indexing:Tuple with o or 1 elementb) Accessing tuple element using sequence unpacking Nesting of tuplesUsage of tupleCommon Operations on tuplesMCQ: Tuples Tuples in Python are similar to lists. A tuple is a sequence of comma separated numbers. A tuple can be decared as a sequence by enclosing or without enclosing them inside the () bracket and all elements are separated with commas. Elements in a tuple can be accessed via unpacking or indexing. Following are the two ways to declare the tuple in Python. a). Declaring and accessing tuple using indexing: >>>tup=(3,4,5,6,7) Or >>> tup=3,4,5,6,7 >>>tup1 (3,4,5,6,7) >>>tup[2] 5 >>>tup(3) 6 Since the tuple is immutable, A tuple does not have the append () or insert () method, nor can it be assigned to another element. >>>tup1=(3,4,5,6,7) >>>tup1[0]= 2 will give an error. TypeError: ‘tuple’ object does not support item assignment Because a tuple is unchangeable, the code is more secure. Therefore, wherever 1possible, use a tuple instead of a list. Tuple with o or 1 element Tuples with 0 or 1 elements is little quirky . They are done as below: tup=() # way of creating empty tuple tup1 = ‘ravi’, #way of creating tuple with 1 element. note the comma at the end. b) Accessing tuple element using sequence unpacking Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence given on right hand side. Example: >>>tup1=(3,4,5,6,7) #tup1 has five elements >>>a,b,c,d,e = tup1 # elements unpacked ans assigned to a,b,c,d,e, So >>>a 3 >>>d 6 Nesting of tuples >>>tup1=(2,3,4,5) >>>tup2=(‘a’, ‘b’, ‘c’) >>>tup3=(tup1, 6,7,tup2) OR >>>tup3=tup1,6,7,tup2 >>>tup3 ((2,3,4,5),6,7,(‘a’, ‘b’, ‘c’)) >>>tup3[0] (2,3,4,5) >>>tup3[3][1] ‘b’ Usage of tuple Tuple is an immutable data value that contains related elements. Tuples are used to group related information and called record in other languages. Such records can represent student or emloyee information. Common Operations on tuples Operation Description Usage Access Elements can be accessed using index count(object) Counts number of occurances of an element >>>X=(1,2,2,3,2,4,2) >>>X.count(2) 3 index() to find the index/position of an element in the tuple. returns the index of the first occurrence in case of multi-occurances of an element >>>X=(1,2,2,3,2,4,2) >>>X.index(4) 5 Join tuples Two or more tuples can be joined into a single tuple tup1 = ( 1, 2, 3 ) tup2 = ( 4, 5, 6 ) tup3 = tup1 + tup2 >>>tup3 (1, 2, 3, 4, 5, 6) Convert string or list to tuple use the tuple() constructor to convert a string or a list to a tuple. You jusst need to pass the string or list to the tuple() construct >>>x=“RAVINDER” >>>tup=tuple(x) >>>tup (‘R’,’A’,’V’,’I’,’N’,’D’,’E’,’R’) a=[6,2,3,9,5] b=tuple(a) >>>b ( 6, 2, 3, 9, 5) convert a list of tuples to a list of strings. Use the list comprehension and join() method we can convert a list of tuples to a list of strings list1 = [ (‘M’, ‘A’, ‘K’, ‘E’), (‘U’, ‘S’, ‘E’), (‘O’, ‘F’) ] # List Comprehension with join() method result = [ ”.join(element) for element in list1 ] print(result) [‘MAKE’, ‘USE’, ‘OF’] min() To find minimum value from tuple >>>tup=(4,2,5,3,8,6) >>>min(tup) 2 max() To find maxmum value from tuple >>>tup=(4,2,5,3,8,6) >>>max(tup) 8 sum() To find sum of all elements in a tuple >>>tup=(4,2,5,3,8,6) >>>sum(tup) 28 sorted() To sort elements of tuple >>>tup=(4,2,5,3,8,6) >>>sorted(tup) (2,3,4,5,6,8) reverse Reverses the elements of a tuple >>>tup=(4,2,5,3,8,6) >>>new_tup=tup[::-1] (6,8,3,5,2,4) Delete (del tuple) Tuple can be eleted using del tuple_name x1=(2,3,4,6,9) del x1 List of tuples to list of lists We can convert list of tuples into a list of list using a for loop >>>list1 = [ (‘A’, ‘B’), (‘C’, ‘D’), (‘E’, ‘F’) ] # List Comprehension >>>result = [ list(element) for element in list1 ] >>>result [[‘A’, ‘B’], [‘C’, ‘D’], [‘E’, ‘F’]] MCQ: Tuple has the same fetching methods as a list. TRUE/FALSE Tuple elements can be accessed using the index. TRUE/ FALSE Tuple is immutable whereas a list is mutable. TRUE /FALSE Tuple is more secured than a List TURE / FALSE The correct way to declare a tuple with only 1 element is: tup1=’r’, Tuples elements can be accessed using indexing and unpacking. TRUE / FALSE Share on Facebook Share Send email Mail Print Print