0% found this document useful (0 votes)
90 views

Pract9

The document discusses dictionary operations in Python. It includes examples of creating, accessing, updating, deleting dictionaries and looping through dictionaries. It also provides exercises on sorting dictionaries, concatenating dictionaries, combining dictionaries and finding unique and highest values in dictionaries.

Uploaded by

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

Pract9

The document discusses dictionary operations in Python. It includes examples of creating, accessing, updating, deleting dictionaries and looping through dictionaries. It also provides exercises on sorting dictionaries, concatenating dictionaries, combining dictionaries and finding unique and highest values in dictionaries.

Uploaded by

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

Subject: PWP[22616]

Practical No9.: Dictionary Operations.


Name: Ritesh Doibale Roll no:41
Implementation:

print("Creating the dictionaries:")


dict1 = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print(dict1)

print("Accessing the dictionaries: ")


print("dict['Name']: ", dict1['Name'])
print("dict1['Age']: ", dict1['Age'])

print("Updating the dict1ionaries")


dict1['Age'] = 8
dict1['School'] = "DPS School"
print("dict1['Age']: ", dict1['Age'])
print("dict1['School']: ", dict1['School'])

print("Deleting items in the dictionaries:")


del dict1['Name']
dict1.clear()
del dict1
print("Deleted")

print("Looping through the dictionaries")


dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
for key, value in dict.items():
print(key, ' - ', value)

Output:
Subject: PWP[22616]
Practical No9.: Dictionary Operations.
Name: Ritesh Doibale Roll no:41
Practical Code:
1.What is the Output following program.
dictionary = {'MSBTE' : 'Maharashtra State Board of Technical Education',
'CO' : 'Computer Engineering',
'SEM' : 'Sixth'
}
del dictionary['CO'];
for key, values in dictionary.items():
print(key)
dictionary.clear();
for key, values in dictionary.items():
print(key)
del dictionary;
for key, values in dictionary.items():
print(key)
Output:

2. What is Output.
dictionary1 = {'Google' : 1,
'Facebook' : 2,
'Microsoft' : 3
}
dictionary2 = {'GFG' : 1,
'Microsoft' : 2,
'Youtube' : 3
}
dictionary1.update(dictionary2);
for key, values in dictionary1.items():
print(key, values)
Output:

3. What is Output.
temp = dict()
temp['key1'] = {'key1' : 44, 'key2' : 566}
temp['key2'] = [1, 2, 3, 4]
for (key, values) in temp.items():
print(values, end = "")
Subject: PWP[22616]
Practical No9.: Dictionary Operations.
Name: Ritesh Doibale Roll no:41
Output:

X.Exercise.
1. Write a Python script to sort (ascending and descending) a dictionary by value.
d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_d = dict(sorted(d.items(), key=lambda item: item[1]))
print("Dictionary in ascending order by value : ", sorted_d)
sorted_d = dict(sorted(d.items(), key=lambda item: item[1], reverse=True))
print("Dictionary in descending order by value : ", sorted_d)
Output:

2. Write a Python script to concatenate following dictionaries to create a new one.


a. Sample Dictionary: b. dic1 = {1:10, 2:20} c. dic2 = {3:30, 4:40} d. dic3 = {5:50,6:60}

dic1 = {1:10, 2:20}


dic2 = {3:30, 4:40}
dic3 = {5:50,6:60}
result = dic1.copy()
result.update(dic2)
result.update(dic3)
print("Concatenated dictionary: ", result)
Output:

3. Write a Python program to combine two dictionary adding values for common keys. a. d1 = {'a':
100, 'b': 200, 'c':300} b. d2 = {'a': 300, 'b': 200, 'd':400}

d1 = {'a': 100, 'b': 200, 'c':300}


d2 = {'a': 300, 'b': 200, 'd':400}
result = {}
for k in d1.keys() | d2.keys():
if k in d1 and k in d2:
result[k] = d1[k] + d2[k]
else:
result[k] = d1.get(k, 0) + d2.get(k, 0)
Subject: PWP[22616]
Practical No9.: Dictionary Operations.
Name: Ritesh Doibale Roll no:41
print("Combined dictionary: ", result)

Output:

4. Write a Python program to print all unique values in a dictionary. a. Sample Data:
[{"V":"S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII":"S005"}, {"V":"S009"},
{"VIII":"S007"}]

data = [{"V":"S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII":"S005"}, {"V":"S009"},
{"VIII":"S007"}]
unique_values = set(val for dic in data for val in dic.values())
for value in unique_values:
print(value)
print(len(unique_values))
Output:

5. Write a Python program to find the highest 3 values in a dictionary.

d = {'a': 100, 'b': 200, 'c': 300, 'd': 400, 'e': 500, 'f': 600, 'g': 700, 'h': 800, 'i': 900}
highest_values = sorted(d.values(), reverse=True)[:3]
for i, value in enumerate(highest_values):
print(f"{value}: ", end="")
for k, v in d.items():
if v == value:
print(k, end=" ")
print()
Output:

You might also like