Basic Programs

If Else Programs

While loop Programs

For loop Programs

List Programs

Dict Programs


Python program to Replace Dictionary Value for the given keys.


In this example shows, we will loop through the dictionary, if the matching key is present, we will update the key. Replace the value. After the loop get over the print.

Example.py
# for the given keys in another dictionary
currency = {'Australia': '$', 'Egypt': '£', 'India': '', 'Japan': '¥', 'Nepal': ''}
update = {"India"  : '', "Nepal" : 'Rs'}

print("Dictionary = ", end = " ")
print(currency)

# Updating dictionary using update values
for i in currency:
    if i in update:
        currency[i]  = update[i]
  
# Printing the dictionary
print("Updated dictionary = ", end = " ")
print(currency)

Output

Dictionary =  {'Australia': '$', 'Egypt': '£', 'India': '$', 'Japan': '¥', 'Nepal': '₦'}

Updated dictionary =  {'Australia': '$', 'Egypt': '£', 'India': '₹', 'Japan': '¥', 'Nepal': 'Rs'}

Prev Next