0% found this document useful (0 votes)
7 views1 page

Practical No 09

The document provides Python scripts to perform various operations on dictionaries, including sorting by value, combining dictionaries, printing unique values, and finding the highest three values. It includes example code snippets for each operation along with their expected outputs. The operations are aimed at enhancing understanding of dictionary manipulation in Python.

Uploaded by

Dhanashri Karad
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)
7 views1 page

Practical No 09

The document provides Python scripts to perform various operations on dictionaries, including sorting by value, combining dictionaries, printing unique values, and finding the highest three values. It includes example code snippets for each operation along with their expected outputs. The operations are aimed at enhancing understanding of dictionary manipulation in Python.

Uploaded by

Dhanashri Karad
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/ 1

Practical No 09

Title: Write Python program to perform following operations on Dictionaries


Class: TYCM-WIN Batch: A Roll No: 16

1. Write a Python script to sort 3. Write a Python program to combine


(ascending and descending) a dictionary two dictionary adding values for
by value. common keys.

def sort_dict_by_value(d): d1 = {'a': 100, 'b': 200, 'c':300}


asc = dict(sorted(d.items(), key=lambda d2 = {'a': 300, 'b': 200, 'd':400}
item: item[1])) d1.update(d2)
print(d1)
desc = dict(sorted(d.items(), Output:
key=lambda item: item[1], reverse=True)) {'a': 300, 'b': 200, 'c': 300, 'd': 400}

return asc, desc 4. Write a Python program to print all


unique values in a dictionary.
sample_dict = {'apple': 3, 'banana': 1,
data = [{"V": "S001"}, {"V": "S002"},
'cherry': 5, 'date': 2}
{"VI": "S001"}, {"VI": "S005"},
{"VII": "S005"}, {"V": "S009"},
ascending, descending =
{"VIII": "S007"}]
sort_dict_by_value(sample_dict)
unique_values = set()
print("Ascending Order:", ascending)
print("Descending Order:", descending)
for dictionary in data:
Ascending Order: {'banana': 1, 'date': 2, for value in dictionary.values():
unique_values.add(value)
'apple': 3, 'cherry': 5}
Descending Order: {'cherry': 5, 'apple': 3, print("Unique Values:", unique_values)
'date': 2, 'banana': 1} Output:
Unique Values: {'S005', 'S009', 'S007',
2. Write a Python script to concatenate 'S001', 'S002'}
following dictionaries to create a new
one.
5. Write a Python program to find the
dict1 = {1:10, 2:20} highest 3 values in a dictionary.
dict2 = {3:30, 4:40}
sample_dict = {'a': 10, 'b': 25, 'c': 30, 'd':
dict3 = {5:50,6:60}
15, 'e': 50, 'f': 40}
merged_dict = {}
merged_dict.update(dict1) top_3_values =
merged_dict.update(dict2) sorted(sample_dict.values(),
merged_dict.update(dict3) reverse=True)[:3]
print("Concatenated Dictionary:",
merged_dict) print("Top 3 highest values:",
Output: top_3_values)
Concatenated Dictionary: {1: 10, 2: 20, 3: Output:
30, 4: 40, 5: 50, 6: 60}
Top 3 highest values: [50, 40, 30]

You might also like