xxxxxxxxxx
# This automatically creates a new element where
# Your key = key, The value you want to input = value
dictionary_name[key] = value
xxxxxxxxxx
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
xxxxxxxxxx
student_scores = {'Simon': 45 }
print(student_scores)
# {'Simon': 45}
student_scores['Sara'] = 63
print(student_scores)
# {'Simon': 45, 'Sara': 63}
xxxxxxxxxx
# empty dictionary
dictionary = {}
# lists
list_1 = [1, 2, 3, 4, 5]
list_2 = ["e", "d", "c", "b", "a"]
# populate a dictionary.
for key, value in zip(list_1, list_2):
dictionary[key] = value
# original
print(f"Original dictionary: {dictionary}")
# Add new item to the dictionary
dictionary[6] = "f"
# Updated dictionary
print(f"updated dictionary: {dictionary}")
xxxxxxxxxx
# define a dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
# add a new key-value pair to the dictionary
my_dict['d'] = 4
print(my_dict) #print as {'a': 1, 'b': 2, 'c': 3, 'd': 4}
# update the value of an existing key
my_dict['a'] = 10
print(my_dict) #print as {'a': 10, 'b': 2, 'c': 3, 'd': 4}
xxxxxxxxxx
Dictionary<Key, Value> dict = new Dictionary<>();
dict.put(typeof(key), typeof(value));