# Create Dictionarya={"1":"Way of Kings","2":"Words of Radiance","3":"Oathbringer","4":"Rhythm of War","0":{"Book":"The Stormlight Archive"}}# Access item using keyprint(a["2"])# printing nested itemprint(a["0"])print(a["0"]["Book"])# Add new itemitems={"5":"New Book","6":"book6"}a.update(items)print(a)
Code to loop through dictionary
books={"1":"Way of Kings","2":"Words of Radiance","3":"Oathbringer","4":"Rhythm of War"}# Loop through the dictionary itemsforkey,valueinbooks.items():# Print the key and value on separate linesprint(f"Key: {key}")print(f"Value: {value}")print()# Add a blank line for readability
Dictionary Method
a={"1":"Way of Kings","2":"Words of Radiance","3":"Oathbringer","4":"Rhythm of War"}# Print all keys of dictionaryprint(a.keys())# print all values of dictionaryprint(a.values())# print all itemsprint(a.items())