Lab. No: 4, Date: 17/02/2023, Day:Friday Topic: List-Tuple-Set-Dict
Lab. No: 4, Date: 17/02/2023, Day:Friday Topic: List-Tuple-Set-Dict
LIST QUESTIONS
4.1)Write a Python program to remove duplicates from a list.
CODE:
n = int(input("Enter number of elements in list: "))
a = []
for i in range(0, n):
a.append(input())
dup_items = set()
uniq_items = []
for x in a:
if x not in dup_items:
uniq_items.append(x)
dup_items.add(x)
print(dup_items)
OUTPUT:
Page 1
T&T Lab.(CS-3096), Spring 2023
4.2)Write a Python function that takes two lists and returns True if they have at least one common
member.
CODE:
def common(list1, list2):
ans = False
for x in list1:
for y in list2:
if x == y:
ans = True
return ans
OUTPUT:
OUTPUT:
Page 2
T&T Lab.(CS-3096), Spring 2023
TUPLE QUESTIONS
4.4) Write a Python program to reverse a tuple.
CODE:
x = ("KIIT UNIVERSITY")
y = reversed(x)
print(tuple(y))
x = (5, 10, 15, 20)
y = reversed(x)
print(tuple(y))
OUTPUT:
OUTPUT:
Page 3
T&T Lab.(CS-3096), Spring 2023
4.6) Write a Python program to calculate the product, multiplying all the numbers in a given tuple.
CODE:
def multiply(nums):
temp = list(nums)
product = 1
for x in temp:
product *= x
return product
nums = (2,4,8,8,9,2,3)
print("Original Tuple: ")
print(nums)
print("Product after multiplying all the numbers of the tuple:", multiply(nums))
OUTPUT:
Page 4
T&T Lab.(CS-3096), Spring 2023
SET QUESTIONS
4.7) Rupal has a huge collection of country stamps. She decided to count the total number of distinct
country stamps in her collection. She asked for your help. You pick the stamps one by one from a stack
of N country stamps.
Find the total number of distinct country stamps.
CODE:
n = int(input("Enter value of N: "))
stamps = set()
for i in range (0,n):
stamps.add(input("Enter stamp country: "))
print("The number of distinct stamps is ",len(stamps))
OUTPUT:
CODE:
a = int(input("Enter number of elements of set A: "))
b = int(input("Enter number of elements of set B: "))
seta = set()
setb = set()
print("Enter elements of set A")
for i in range(0,a):
seta.add(input())
print("Enter elements of set B")
for i in range(0,b):
setb.add(input())
print(seta.issubset(setb))
Page 5
T&T Lab.(CS-3096), Spring 2023
OUTPUT:
CODE:
setA = set()
setB = set()
a = int(input("Enter the number of elements in set A:"))
b = int(input("Enter the number of elements in set B:"))
for i in range(0,a):
setA.add(input())
for i in range(0,b):
setB.add(input())
print("Original sets:")
print(setA)
print(setB)
setC = setA.union(setB)
print("\nUnion of above sets:")
print(setC)
OUTPUT:
Page 6
T&T Lab.(CS-3096), Spring 2023
DICTIONARY QUESTIONS
4.10) Write a Python script to check whether a given key already exists in a dictionary.
CODE:
d = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 8: 80, 9: 90, 10: 100}
def keyPresent(x):
if x in d:
print('Key is present in the dictionary')
else:
print('Key is not present in the dictionary')
keyPresent(5)
keyPresent(7)
keyPresent(9)
keyPresent(12)
OUTPUT:
4.11) Write a Python program to iterate over dictionaries using for loops.
CODE:
d = {'x': 10, 'y': 20, 'z': 30, 'a': 64, 'b': 98, 'c': "hello", 'd': "world"}
for dict_key, dict_value in d.items():
print(dict_key, ':', dict_value)
OUTPUT:
Page 7
T&T Lab.(CS-3096), Spring 2023
CODE:
gadgets = {'smartphone': 20000,
'laptop': 60000,
'headphones': 2000,
'keyboard': 500}
sorted_values = sorted(gadgets.values())
sorted_dict = {}
for i in sorted_values:
for k in gadgets.keys():
if gadgets[k] == i:
sorted_dict[k] = gadgets[k]
print(sorted_dict)
OUTPUT:
Page 8