xxxxxxxxxx
# welcome to softhunt.net
# Creating an empty Dictionary
Dictionary = {}
print("Empty Dictionary: ", Dictionary)
# Adding elements one at a time
Dictionary[0] = 'Softhunt'
Dictionary[1] = '.net'
print("\nDictionary after adding 2 elements: ", Dictionary)
# Adding set of values
# to a single Key
Dictionary['Value_set'] = 2, 3, 4
print("\nDictionary after adding 3 elements: ", Dictionary)
# Updating existing Key's Value
Dictionary[2] = 'Greetings'
print("\nUpdated key value: ", Dictionary)
# Adding Nested Key value to Dictionary
Dictionary[3] = {'Nested' :{'i' : 'Hello', 'ii' : 'User'}}
print("\nAdding a Nested Key: ", Dictionary)
xxxxxxxxxx
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
xxxxxxxxxx
myDictionary = {
"first": "A",
"second": "B",
"third": "C",
}
myDictionary["fourth"] = "D"
print(myDictionary)
# Output:
# {'first': 'A', 'second': 'B', 'third': 'C', 'fourth': 'D'}
xxxxxxxxxx
mydict = {'score1': 41,'score2': 23}
mydict['score3'] = 45 # using dict[key] = value
print(mydict)
xxxxxxxxxx
# This automatically creates a new element where
# Your key = key, The value you want to input = value
dictionary_name[key] = value
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));