0% found this document useful (0 votes)
89 views8 pages

Lab. No: 4, Date: 17/02/2023, Day:Friday Topic: List-Tuple-Set-Dict

This document is a lab report from the Tools & Technique Laboratory (CS-3096) course. It includes 12 questions on lists, tuples, sets and dictionaries in Python. For each question, it provides the code written by the student to solve the problem and the output of running the code. The questions cover a range of tasks like removing duplicates from a list, checking for common members between lists, calculating frequency of elements in a list, reversing a tuple, converting a string to a tuple, multiplying numbers in a tuple, finding distinct elements in a set, checking subset relationships between sets, performing set operations, checking for keys in a dictionary, iterating over dictionaries, and sorting a dictionary by values.

Uploaded by

Dristant Panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views8 pages

Lab. No: 4, Date: 17/02/2023, Day:Friday Topic: List-Tuple-Set-Dict

This document is a lab report from the Tools & Technique Laboratory (CS-3096) course. It includes 12 questions on lists, tuples, sets and dictionaries in Python. For each question, it provides the code written by the student to solve the problem and the output of running the code. The questions cover a range of tasks like removing duplicates from a list, checking for common members between lists, calculating frequency of elements in a list, reversing a tuple, converting a string to a tuple, multiplying numbers in a tuple, finding distinct elements in a set, checking subset relationships between sets, performing set operations, checking for keys in a dictionary, iterating over dictionaries, and sorting a dictionary by values.

Uploaded by

Dristant Panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

T&T Lab.

(CS-3096), Spring 2023

Tool & Technique Laboratory (T&T Lab.)


[CS-3096]
Individual Work
Lab. No: 4 , Date: 17/02/2023 , Day:Friday
Topic: LIST-TUPLE-SET-DICT
Roll Number: 2006480 Branch/Section: IT-3
Name in Capital: Dristant Panda

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

print(common([1, 2, 3, 4, 5], [5, 6, 7, 8, 9]))


print(common([1, 2, 3, 4, 5], [6, 7, 8, 9]))

OUTPUT:

4.3)Write a Python program to get the frequency of elements in a list.


CODE:
import collections
a = []
n = int(input("Enter number of elements in list: "))
for i in range(0, n):
a.append(input())

print("Original List : ", a)


ctr = collections.Counter(a)
print("Frequency of the elements in the List : ", ctr)

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:

4.5) Write a Python program to convert a given string list to a tuple.


CODE:
def strToTup(str):
result = tuple(x for x in str if not x.isspace())
return result

str = "Tools & Techniques Lab"


print("Original string: ")
print(str)
print(type(str))
print("The given string as a tuple:")
print(strToTup(str))
print(type(strToTup(str)))

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:

4.8) You are given two sets A, and B.


Your job is to find whether set A is a subset of set B.

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:

4.9) Write a Python program to create a union of sets.

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

4.12) Write a Python program to sort a given dictionary by values.

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

You might also like