Dictionary

To add a key-value pair to a blank dictionary
# Create a blank dictionary my_dict = {} # Add a key-value pair my_dict["key"] = "value" # Now my_dict contains {"key": "value"}

We have a dictionary as below

student_scores = {
"Harry": 81,
"Ron": 78,
"Hermione": 99,
"Draco": 74,
"Neville": 62,
}
If we use
for student in student_scores:
score = student_scores[student]
print(score)
The result would be:
81
78
99
74
62
if we use:
for student in student_scores:
score = student_scores[student]
print(student)
The result would be:
Harry
Ron
Hermione
Draco
Neville
if we use:
print(student_scores)
The result would be:
{'Harry': 81, 'Ron': 78, 'Hermione': 99, 'Draco': 74, 'Neville': 62}


How to add dictionary to a list
# Create a list
my_list = []

# Create a dictionary
my_dict = {"key": "value"}

# Add the dictionary to the list
my_list.append(my_dict)

# Now my_list contains the dictionary
How to get the max value in a dictionary
max_value = max(dictionary.value())


Comments