Dictionary manipulation

# Dictionary manipulation

# Creating a dictionary
# Empty dictionary

dict1 = {}

# Adding elements to dictionary

Nterms = int(input("Enter number of items : "))

for idx in range(0,Nterms,1):
k = input("Enter key : ")
v = int(input("Enter value : "))

dict1[k] = v

print("Dictionary : ")
print(dict1)
print("Length of dictionary = ", len(dict1))

# Updating dictionary

k = input("Enter key to update : ")
if (k not in dict1):
print("Key not found in dictionary.")
else:
v = int(input("Enter new value : "))
dict1[k] = v

print("Dictionary : ")
print(dict1)

print("Removing all elements from the dictionary")
dict1.clear()
print(dict1)


No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu