TUPLE Author: CloudVikas Published Date: 7 December 2020 Welcome to TUPLE How will you find out count of tuples occurrence in list of tuples? which option will you give correct result?Input = [[('a', 'c')],[('a', 'b')], [('a', 'b')]] for x in Input: Output[x[0]] += 1 print(Output) Using counter and chain: Output = Counter(chain(*Input)) Output = Counter((*Input)) How will you sort a list of tuples by first Item?Input : [('45', 10), ('25', 5)] Output : [('25', 5), ('45', 10)] Using the Bubble Sort way,def Sort(tupll): lst = len(tupl) for i in range(0, lst): for j in range(0, lst-i-1): if (tupl[j][0] > tupl[j + 1][0]): temp = tupl[j] tupl[j]= tupl[j + 1] tupl[j + 1]= temp return tupl def Sort_Tuple(tup): tup.sort(key = lambda x: x[0]) return tup print(Sort_Tuple(tup)) def Sort_Tuple(tup): tup.sort(key = lambda x: x[2]) return tup print(Sort_Tuple(tup)) Using the Bubble Sort way,def Sort(tupll): lst = len(tupl) for i in range(0, lst): for j in range(0, lst-i-1): if (tupl[j][0] < tupl[j + 1][0]): temp = tupl[j] tupl[j]= tupl[j + 1] tupl[j + 1]= temp return tupl How to sort a list of tuples alphabetically?Input: [("aaaa", 2), ("aa", 3), ("bab", 9)]Output: [('aa', 3), ('aaaa', 2), ('bab', 9)] Using Bubble sort : def SortTuple(tup): n = len(tup) for i in range(n): for j in range(n-i-1): if tup[j][0] > tup[j + 1][0]: tup[j], tup[j + 1] = tup[j + 1], tup[j] return tup Using sort() method: def SortTuple(tup): tup.sort(key = lambda x: x[0]) return tup print(SortTuple(tup)) Using sorted() method: def Sort_Tuple(tup): return(sorted(tup, key = lambda x: x[0])) print(Sort_Tuple(tup)) def Sort_Tuple(tup): return(sorted(tup, key = lambda x: x[1])) print(Sort_Tuple(tup)) Find out count of tuples occurrence in list of tuplesimport collections Output = collections.defaultdict(int) Input = [[('a', 'c')],[('a', 'b')], [('a', 'b')]] for elem in Input: Output[elem[0]] += 1print(Output)Truefalse John has bought 5 products. we have a list which contains product name and its price. Create one tuple which is sorted based on price.lst = [('Key', 101), ('Lock', 320), ('Hammer', 100), ('Spanner', 67), ('wire', 93)]import operatorprint(sorted(lst, reverse = True, key = operator.itemgetter(1)))will it give the correct result?yesno Write a program to remove empty tuple from a list of tuples.lst1= [(), ('vikas', 5), ('mohan', 11), (), ('Harsha', 115),()] lst2 = [] for item in lst1: if len(item) != 0: lst2.append(item) print(lst2)Yes Write a program to create following 2 lists: - a list of names - a list of roll numbersGenerate and print a list of tuples containing name, roll number from the 2 lists. From this list generate 2 tuples—one containing all names, another containing all roll numbers. name = ['vikas', 'Mohan', 'Aditya'] rollno = ['12', '42', '40'] t1 = tuple(name) print(t1) lst = [t1, t2, t3] print(lst) t2 = tuple(rollno) print(t2) Time is Up! Time's up Author: CloudVikas