Member-only story
Python Programming Language: How To Modify Elements Within a Dictionary
Hi everyone, welcome back. In these examples, we will be going over how to modify elements from within a dictionary in Python. Dictionaries can be used to store a collection of data. Dictionaries use key-value pairs to store data compared to using index numbers. Dictionaries are mutable and does not allow for duplicate keys. For Python 3.7 and higher, dictionaries are ordered whereas in earlier versions, dictionaries are unordered. With this introduction out of the way, let’s get into it.
Creating Dictionary
Let’s start by creating our dictionary:
myDictionary = {
"first": "A",
"second": "B",
"third": "C",
}
print(myDictionary)Output:
{'first': 'A', 'second': 'B', 'third': 'C'}
So now, we have a populated dictionary with a few elements. We can verify that it was successfully created and populated by printing the dictionary and checking the output. Now let’s see how we can modify the elements within the dictionary.
Modifying Elements Within The Dictionary
Let’s try to modify an element within the dictionary. We can change a value by assigning a new value to a key within the dictionary. Let’s see an example: