Question 38: 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"}]. Download hole Program / Project code, by clicking following link: How to Create Dictionary, Access Dictionary elements, Update Dictionary, Delete Dictionary, Looping through Dictionary ? Certainly! Here's a Python program that demonstrates various operations on dictionaries, including creating a dictionary, accessing elements, updating elements, deleting the dictionary, and looping through the dictionary:
# # Create a dictionary
student_info = {
'name': 'John',
'age': 25,
'grade': 'A',
'courses': ['Math', 'Science', 'History']
}
# Access dictionary elements
print("Name:", student_info['name'])
print("Age:", student_info['age'])
print("Courses:", student_info['courses'])
# Update dictionary elements
student_info['age'] = 26
student_info['courses'].append('English')
# Access and print updated values
print("\nUpdated Age:", student_info['age'])
print("Updated Courses:", student_info['courses'])
# Loop through the dictionary and print key-value pairs
print("\nDictionary Elements:")
for key, value in student_info.items():
print(f"{key}: {value}")
# Delete the dictionary
del student_info
# Attempt to access the dictionary after deleting it (will raise an error)
# print(student_info) # Uncommenting this line would result in an error
In this program: - We create a dictionary student_info containing information about a student.
- We access dictionary elements using keys and print them.
- We update the age and add a new course to the dictionary.
- We loop through the dictionary using a for loop to print all key-value pairs.
- We delete the dictionary using the del statement. After deletion, attempting to access the dictionary will result in an error.
Programming Code: Following code write in: BP_P38.py # Print all unique values in a dictionary
# [{"V": "S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII": "S005"},
# {"V": "S009"}, {"VII": "S007"}]
dic1 = [{"V": "S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII": "S005"},
{"V": "S009"}, {"VII": "S007"}]
print("Dic1 is: ", dic1)
print("type of Dic1 is", type(dic1))
# unique values
u_value = set(val for dic in dic1 for val in dic.values())
print("Unique Values: ", u_value)
print("type of Dic1 is", type(u_value))
# Thanks for Reading
Output:
How to Create Dictionary, Access Dictionary elements, Update Dictionary, Delete Dictionary, Looping through Dictionary ?
Certainly! Here's a Python program that demonstrates various operations on dictionaries, including creating a dictionary, accessing elements, updating elements, deleting the dictionary, and looping through the dictionary:
# # Create a dictionary student_info = { 'name': 'John', 'age': 25, 'grade': 'A', 'courses': ['Math', 'Science', 'History'] } # Access dictionary elements print("Name:", student_info['name']) print("Age:", student_info['age']) print("Courses:", student_info['courses']) # Update dictionary elements student_info['age'] = 26 student_info['courses'].append('English') # Access and print updated values print("\nUpdated Age:", student_info['age']) print("Updated Courses:", student_info['courses']) # Loop through the dictionary and print key-value pairs print("\nDictionary Elements:") for key, value in student_info.items(): print(f"{key}: {value}") # Delete the dictionary del student_info # Attempt to access the dictionary after deleting it (will raise an error) # print(student_info) # Uncommenting this line would result in an errorIn this program:
- We create a dictionary student_info containing information about a student.
- We access dictionary elements using keys and print them.
- We update the age and add a new course to the dictionary.
- We loop through the dictionary using a for loop to print all key-value pairs.
- We delete the dictionary using the del statement. After deletion, attempting to access the dictionary will result in an error.
# Print all unique values in a dictionary # [{"V": "S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII": "S005"}, # {"V": "S009"}, {"VII": "S007"}] dic1 = [{"V": "S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII": "S005"}, {"V": "S009"}, {"VII": "S007"}] print("Dic1 is: ", dic1) print("type of Dic1 is", type(dic1)) # unique values u_value = set(val for dic in dic1 for val in dic.values()) print("Unique Values: ", u_value) print("type of Dic1 is", type(u_value)) # Thanks for Reading
Output:
0 Comments