How Do You Update A Dictionary In Python?

Hello Pythonistas welcome back.

Today, we will see how to update a dictionary once it’s created.


Somethings To Know Before We Update A Dictionary

  • only values in a python dictionary are mutable not keys.
    • Because dictionaries are not like lists where you stored data in order.
    • The only way to access values is through keys that’s why they are immutable.
  • A Python dictionary is an unordered sequence.

How do I add a new key-value to an existing dictionary?

Let’s say along with apple🍎, banana🍌 and spinach🥦 now you are going to keep pineapples🍍, kiwi🥝, and oranges🍊 too in your store.

This means you need to add their location here in the dictionary where_is_what. This is how:-

where_is_what = {"apple": "top row of shelf",
"banana": "bottom row of the shelf",
"spinach": "middle row of the shelf"}
print(f"Before:- {where_is_what}\n")
where_is_what["pineapples"]="top left corner"
where_is_what["kiwi"]="top right corner"
where_is_what["oranges"]="bottom left corner"
print(f"After:- {where_is_what}")

Output:-

Before:- {'apple': 'top row of shelf', 'banana': 'bottom row of the shelf', 'spinach': 'middle row of the shelf'}
After:- {'apple': 'top row of shelf', 'banana': 'bottom row of the shelf', 'spinach': 'middle row of the shelf', 'pineapples': 'top left corner', 'kiwi': 'top right corner', 'oranges': 'bottom left corner'}

Can we change key-value in dictionary?

Now you thought that you must keep spinach 🥦 in the top row.

Apples🍎, pineapples🍍, and bananas🍌 should be in the second row. And kiwi 🥝and oranges🍊 in the bottom row.

This means you need to update the already existing data. Here’s how you will do it:-

print(f"Before:- {where_is_what}\n")
where_is_what["spinach"]="top row"
where_is_what["apple"]="middle row left corner"
where_is_what["pineapples"]="middle row center"
where_is_what["banana"]= "middle row right corner"
where_is_what["kiwi"]="bottom row left corner"
where_is_what["oranges"]="bottom row right corner"
print(f"After:- {where_is_what}")

Output:-

Before:- {'apple': 'top row of shelf', 'banana': 'bottom row of the shelf', 'spinach': 'middle row of the shelf', 'pineapples': 'top left corner', 'kiwi': 'top right corner', 'oranges': 'bottom left corner'}
After:- {'apple': 'middle row left corner', 'banana': 'middle row right corner', 'spinach': 'top row', 'pineapples': 'middle row center', 'kiwi': 'bottom row left corner', 'oranges': 'bottom row right corner'}

How do I remove a key and value from a dictionary?

You just realized that kiwi🥝 and oranges🍊 don’t have a good enough demand and decided to remove them from your shop.

This is how you will record these changes in your where_is_what:-

print(f"Before:- {where_is_what}\n")
del where_is_what["kiwi"]
del where_is_what["oranges"]
print(f"After:- {where_is_what}")

Output:-

Before:- {'apple': 'middle row left corner', 'banana': 'middle row right corner', 'spinach': 'top row', 'pineapples': 'middle row center', 'kiwi': 'bottom row left corner', 'oranges': 'bottom row right corner'}
After:- {'apple': 'middle row left corner', 'banana': 'middle row right corner', 'spinach': 'top row', 'pineapples': 'middle row center'}

Leave a Reply