In this post, we are going to explored most asked python Python 25 Tuple programming interview questions and answers
1. How to Sort List Of Tuples By The First Element
Using Sort() function
lang_listtuple= [('C#',1), ('Go',7), ('Basic',8), ('Python',60)]
print('original list of tuples =\n',lang_listtuple)
lang_listtuple.sort()
print('after sorting =\n',lang_listtuple)
Output
original list of tuples =
[('C#', 1), ('Go', 7), ('Basic', 8), ('Python', 60)]
after sorting =
[('Basic', 8), ('C#', 1), ('Go', 7), ('Python', 60)]
2. How to Sort List Of Tuples By Second Element
lang_listtuple= [('C#',1), ('Go',7), ('Basic',8), ('Python',60)]
print('original list of tuples =\n',lang_listtuple)
lang_listtuple.sort(key = lambda item:item[1])
print('after sorting by second item=\n',lang_listtuple)
Output
the original list of tuples =
[('C#', 1), ('Go', 7), ('Basic', 8), ('Python', 60)]
after sorting by the second item =
[('C#', 1), ('Go', 7), ('Basic', 8), ('Python', 60)]
3. How to remove duplicate from list of tuples
Using List comprehension
#a list of tuples
list_of_tuples= [("C#", 1), ("C#", 1), ("C++", 3),
("C++", 3), ("Python", 25), ("Rust", 30)]
#removing the duplicate
unique_list_of_tuples=list(set([item for item in list_of_tuples]))
print('list of tuple after removing duplicate=\n', unique_list_of_tuples)
Output
array one is linear [1 2 3 4]
4. How to remove duplicate from list of tuples using dict() function
listofTuples = [("C#" , 2), ("JS" , 3), ("C#" , 2),("GO" , 23),("Java" , 77)]
dictiwithlistof_tuples = dict(listofTuples)
print(dictiwithlistof_tuples)
Output
{'C#': 2, 'JS': 3, 'GO': 23, 'Java': 77}
5. How do we reverse a string in Python?
String = "String Example" [::-1]
print(String)
Output
elpmaxE gnirtS
6. Example to convert list to tuple.
listelements = ['C#', 'Java', 'Go', 'Rust']
tuple = tuple(listelements)
print(tuple)
#Output:
('C#', 'Java', 'Go', 'Rust')
7. How to convert two list to dictionary in Python?
Subj = ["Math", "English", "computer", "Music"]
marks = [98, 90, 75, 100]
student_score = dict(zip(Subj, marks))
print(student_score)
#Output:{'Math': 98, 'English': 90, 'computer': 75, 'Music': 100}
8. How To Filter A List of Given Elements In a List Of Tuples?
Using lambda and list
listoftuple = [(1,2,'C#'),('Pyth',4),('Pyth',5,6),(7,8,),('Pyth',6)]
item = 'Pyth'
output = list(filter(lambda x:item in x, listoftuple))
#filltering the list of tuple
print('fliterring list of tuples =\n',output)
Output
fliterring list of tuples =
[('Pyth', 4), ('Pyth', 5, 6), ('Pyth', 6)]
9. Different Ways To Concatenate Tuples In Python.
Sum() : To concatenate tuples into single/nested Tuples
Example :Sum() to concatenate tuples into a single tuple
tupleint= (1,2,3,4,5,6)
langtuple = ('C#','C++','Python','Go')
#concatenate the tuple
tuples_concatenate = sum((tupleint, langtuple), ())
print('concatenate of tuples \n =',tuples_concatenate)
Output
concatenate of tuples
= (1, 2, 3, 4, 5, 6, 'C#', 'C++', 'Python', 'Go')
Sum() to concatenate tuple into a nested tuple
Now let us understand how we can sum tuples to make a nested tuple.In this program we will use two tuples which are nested tuples, please note the ‘,’ at the end of each tuples tupleint and langtuple.
tupleint= (1,2,3,4,5,6),
langtuple = ('C#','C++','Python','Go'),
#concatenate the tuple
tuples_concatenate = sum((tupleint, langtuple), ())
print('concatenate of tuples \n =',tuples_concatenate)
Output
concatenate of tuples
= ((1, 2, 3, 4, 5, 6), ('C#', 'C++', 'Python', 'Go'))
10. Write a program to create mixed datatype tuple in Python
Langtuple = (1, "C#", 3.4,'C++',False,True)
print('mixed type tuple=',Langtuple)
Output
mixed type tuple= (1, 'C#', 3.4, 'C++', False, True)
11. What will be the output of tuple slicing program .
Langtuple = (1, "C#", 3.4,'C++',False,True)
print( Langtuple[::5])
print( Langtuple[::2])
print( Langtuple[:-3])
Output
(1, True)
(1, 3.4, False)
(1, 'C#', 3.4)
12. How to replace the last tuple value from list of tuples.
listoftuple = [(1, "C#",40), (3.4,'C++',False),(True,0,56)]
print([tuple[:-1] + (45,) for tuple in listoftuple])
Output
[(1, 'C#', 45), (3.4, 'C++', 45), (True, 0, 45)]
13. How to remove empty tuple from list of tuples.
listoftuple = [(1, "C#",40),(),('',),(3.4,'C++',False),(True,0,56),()]
print([tuple for tuple in listoftuple if tuple])
Output
[(1, 'C#', 40), ('',), (3.4, 'C++', False), (True, 0, 56)]
14. How to convert list of tuple to dictionary
#defining an empty dictionary
lang_dictionary = {}
lang_tuples= [("C#", 1), ("C", 2), ("C++", 3), ("Go", 20), ("Python", 25), ("Rust", 30)]
for lang,value in lang_tuples:
#list of tuples to dictionary
lang_dictionary.setdefault(lang, []).append(value)
print('list of tuples to dictionary=\n', lang_dictionary)
Output
list of tuples to dictionary=
{'C#': [1], 'C': [2], 'C++': [3], 'Go': [20], 'Python': [25], 'Rust': [30]}
15.How to sort the tuple by float value from list of tuples in ascending order.
listoftuple = [('C#', '10.20'), ('Go', '13.06'),('F#','10.05'), ('Python', '15.5')]
print( sorted(listoftuple, key=lambda ele: float(ele[1])))
Output
[('F#', '10.05'), ('C#', '10.20'), ('Go', '13.06'), ('Python', '15.5')]
16. Write code to unzip a list of tuple in separate lists
listoftuple = [('C#', '10.20'), ('Go', '13.06'),('F#','10.05'), ('Python', '15.5')]
print(list(zip(*listoftuple)))
Output
[('C#', 'Go', 'F#', 'Python'), ('10.20', '13.06', '10.05', '15.5')]
17. How to find a Tuple element by value in Python
tuple = ('C#', 10, 'Go', '13.06','F#','10.05','Python', '15.5')
item =10;
if item in tuple:
print("Element in Tuple")
else:
print("Element not in Tuple")
Output
Element in Tuple
18. Code to find the positive elements tuple from the list of Tuples
list_of_tuples = [(12, 3, 10), (-15, 6, 9), (-7, 2, 7)]
# all() method to check each item of listoftuples
res = [pos for pos in list_of_tuples if all(item >= 0 for item in pos)]
# result
print("Tuples contain positive items : " ,res)
Output
Tuples contain positive items : [(12, 3, 10)]
19.How To Do The Position Sum Of Python Tuple Elements?
numtuple = (5, 6, 7)
inttuples = (8, 9, 10,11)
# printing both tuples
print('num tuple =', numtuple)
print("langtuple : ",inttuples)
# tuple using map() and lambda
added_tuples = tuple(map(lambda num, num1: num + num1, numtuple, inttuples))
print("tuples addition using map and lambda =" ,added_tuples)
Output
num tuple = (5, 6, 7)
langtuple : (8, 9, 10, 11)
tuples addition using map and lambda = (13, 15, 17)
20.How to find a tuple that has all elements divide by any number from a list of tuples.
list_of_tuples = [(12, 8, 16), (24, 16, 28), (7, 2, 7)]
number = 4
# all() method to check each item of listoftuples
res = [div for div in list_of_tuples if all(item%number == 0 for item in div)]
print("Tuple which all items divide by number 4 = : ",res)
Output
Tuple which all items divide by number 4 = : [(12, 8, 16), (24, 16, 28)]
21.How to Convert Tuple to string.
str_tuple = ('This', 'is', 'tuple', 'to', 'string')
tuple_to_string = ''.join(str_tuple)
print(tuple_to_string)
Output
Thisistupletostring
22. Program to remove duplicate from List of Tuples
lst_of_tuple= [('C#', 1), ('JS', 2), ('C#', 1), ('JS', 2),('React',1)]
result = [tup for tup in (set(tuple(ele) for ele in lst_of_tuple))]
print(result)
Output
[('C#', 1), ('JS', 2), ('React', 1)]
23.How to find the Duplicate tuple in list of tuple
lst_of_tuple= [('C#', 1), ('JS', 2), ('C#', 1), ('JS', 2),('React',1)]
#finding the duplicate tuple
result_tuple = list(set([item for item in lst_of_tuple
if lst_of_tuple.count(item) > 1]))
print(result_tuple)
Output
[('C#', 1), ('JS', 2)]
24. How to get the max value tuple from List of Tuple.
from operator import itemgetter
lst_of_tuple= [('C#', 100), ('JS', 200),('React',400)]
#fmax value tuple key
result_tuple = max(lst_of_tuple, key = itemgetter(1))[0]
print(result_tuple)
Output
React
25. How to get min and max key-value pair from the list of Tuples.
lst_of_tuple= [('C#', 100), ('JS', 200),('React',400)]
#key - value with max ,min value
max_key_value = max(lst_of_tuple)[0], max(lst_of_tuple)[1]
min_key_value = min(lst_of_tuple)[0],min(lst_of_tuple)[1]
print(max_key_value,min_key_value)
Output
('React', 400) ('C#', 100)
26.Program to generate random number in Python
import random
#Generate random number between 0and 9
print(random.randint(0,9))
#Output: 4
choice() function
import random
# choice() function to generate function from list
print ("Generate number num from list : ")
print (random.choice([1, 4, 8, 10, 3]))
#Output :Generate number num from list :
4