Date: 27/2/26 23D411 – PYTHON PROGRAMMING
LABORATORY
Exp No: 06 Dictionary datatype: Creation, Operations,
Methods
AIM :
To understand and implement the creation of dictionaries, perform
dictionary operations, and use built-in dictionary methods in Python.
THEORY :
A dictionary is a collection of data stored in key: value pairs. It is written
using curly braces { }. Keys must be unique and immutable (int, string, tuple).
Values can be of any data type. Dictionaries are mutable.
Method Purpose Example
keys() Return all keys [Link]()
values() Return all values [Link]()
items() Return key value [Link]()
pairs
get () Access safely [Link](“age”)
Update() Add/modify items [Link]({“age”:22})
pop () Remove by key [Link](“age”)
clear () Remove all items [Link]()
copy () Copy dictionary [Link]()
PROCEDURE
1. Open the Python IDE and create a new Python program file.
2. Create a dictionary using curly braces {} and assign key–value pairs.
3. Access dictionary values using keys.
4. Use a loop to display all key–value pairs in the dictionary.
5. Execute the program and observe the output.
PROGRAMS
PROBLEM 1 :
Write a Python program to count how many times each student appears
in the attendance list using dictionary datatype
CODE :
names = input("Enter student names separated by space: ").split()
attendance = {}
for n in names:
attendance[n] = [Link](n, 0) + 1
print(attendance)
OUTPUT :
PROBLEM 2 :
Write a Python program to count the frequency of each character in a given
string using dictionary. Sample Input: apple Sample Output: {'a': 1, 'p': 2, 'l': 1,
‘e’:1}
CODE :
text = input("Enter a string: ")
freq = {}
for ch in text:
freq[ch] = [Link](ch, 0) + 1
print(freq)
OUTPUT :
PROBLEM 3 :
Write a Python program to increase each product price by ₹2 using dictionary.
Sample Input: {'Pen': 10, 'Pencil': 5, 'Eraser': 3} Sample Output: {'Pen': 12,
'Pencil': 7, 'Eraser': 5}
CODE :
n = int(input("Enter number of products: "))
products = {}
for i in range(n):
name = input("Enter product name: ")
price = float(input("Enter price: "))
products[name] = price
for k in [Link]():
products[k] += 2
print(products)
OUTPUT :
PROBLEM 4 :
Write a Python program to retrieve a patient’s age from the given dictionary.
{"Name": "Alice", "Blood Group": "O+", "Age": 35}
CODE :
patient = {"Name": "Alice", "Blood Group": "O+", "Age": 35}
print("Patient Age:", [Link]("Age"))
OUTPUT :
PROBLEM 5 :
Write a Python program to update a patient’s vital signs dictionary by adding a
new vital parameter (Temperature). After updating the original dictionary, create
a copy of it and print both the updated original dictionary and the copied
dictionary.
CODE :
vitals = {}
n = int(input("Enter number of parameters: "))
for i in range(n):
k = input("Enter parameter: ")
v = float(input("Enter value: "))
vitals[k] = v
temp = float(input("Enter Temperature: "))
vitals["Temperature"] = temp
copy_vitals = [Link]()
print("Updated Original:", vitals)
print("Copy:", copy_vitals)
OUTPUT :
PROBLEM 6 :
Write a Python program to remove a specific patient ID from the ICU patient
dictionary.
CODE :
icu = {}
n = int(input("Enter number of patients: "))
for i in range(n):
pid = input("Enter ID: ")
name = input("Enter name: ")
icu[pid] = name
rid = input("Enter ID to remove: ")
if rid in icu:
removed=[Link](rid)
print(“Removed ID and Name:”,rid,removed)
else:
print("ID not found")
print("Updated ICU:", icu)
OUTPUT :
PROBLEM 7 :
Write a Python program to find the patient with the highest blood glucose level in
a dictionary datatype.
CODE :
glucose = {}
n = int(input("Enter number of patients: "))
for i in range(n):
name = input("Enter patient name: ")
level = float(input("Enter glucose level: "))
glucose[name] = level
highest = max(glucose, key=[Link])
print("Highest:", highest, ", glucose level:",glucose[highest],"mg/dl")
OUTPUT :
PROBLEM 8 :
Write a Python program to merge two dictionaries containing patient test
counts. If a test appears in both, add their values.
CODE :
d1 = {}
d2 = {}
n1 = int(input("Entries in dict1: "))
for i in range(n1):
k = input("Enter key: ")
v = int(input("Enter value: "))
d1[k] = v
n2 = int(input("Entries in dict2: "))
for i in range(n2):
k = input("Enter key: ")
v = int(input("Enter value: "))
d2[k] = v
merged = {}
for d in (d1, d2):
for k, v in [Link]():
merged[k] = [Link](k, 0) + v
print(merged)
OUTPUT :
PROBLEM 9 :
A vegetable vendor wishes to store the vegetables she sells in her shop along
with the price/kg. She stores at the maximum five vegetable prices. Write a
python script to achieve this. To this add a new vegetable with its price and also
print the cost of ‘Brinjal’ - print ‘Zero’ if not available. Finally update the cost of
‘onion’.
CODE :
veg = {}
n = min(5, int(input("Enter number of vegetables (max 5): ")))
for i in range(n):
name = input("Enter vegetable name: ").lower()
price = float(input("Enter price/kg: "))
veg[name] = price
newveg = input("Enter new vegetable: ").lower()
newprice = float(input("Enter price: "))
veg[newveg] = newprice
print("Brinjal cost:", [Link]("brinjal", 0))
if "onion" in veg:
veg["onion"] = float(input("Enter updated onion price: "))
print(veg)
OUPUT:
PROBLEM 10 :
Write a Python script to concatenate following dictionaries to create a new one.
Sample Input: dict1={1:10, 2:20} dict2={3:30, 4:40} dict3={5:50,6:60}
CODE :
def get_dict(n):
d = {}
for i in range(n):
k = int(input("Enter key: "))
v = int(input("Enter value: "))
d[k] = v
return d
dict1 = get_dict(int(input("Entries in dict1: ")))
dict2 = get_dict(int(input("Entries in dict2: ")))
dict3 = get_dict(int(input("Entries in dict3: ")))
new = {}
for d in (dict1, dict2, dict3):
[Link](d)
print("Concatenated dict:",new)
OUTPUT :
RESULT
Thus, dictionary creation, operations, and methods were successfully
implemented and verified using Python.
LAB QUESTIONS
1. How to create an empty dictionary?
2. Can a dictionary have duplicate keys?
3. Are dictionaries ordered?
4. How to access a value?
5. What is the method to get all keys?