The dictionary explained that the keys are immutable and cannot be manipulated, but the values are mutable and can be manipulated. This time I would like to explain the operation of the dictionary.
First, let's talk about adding elements. Enter the following code from the ** Python Console **.
>>> foodD = {'a' : 'apple', 'b' : 'banana', 'c': 'cake'}
>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'cake'}
Up to this point, as explained last time, it is the creation of a dictionary. Now, let's add an element to the dictionary variable ** foodD **, but there are two ways to add it.
First, I will explain how to add one element. Enter the following code in the ** Python Console **. Display the contents of the variable ** foodD ** once and then execute.
>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'cake'}
>>> foodD['d'] = 'dragon fruits'
>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'cake', 'd': 'dragon fruits'}
When adding an element to the dictionary, add a new element in the form of ** variable name [key] = value **.
But what if you already have the key? Enter the following code in the ** Python Console **. Display the contents of the variable ** foodD ** once and then execute.
>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'cake', 'd': 'dragon fruits'}
>>> foodD['c'] = 'carrot'
>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'carrot', 'd': 'dragon fruits'}
You can see that the **'c' ** part has been overwritten. In fact, you cannot specify ** duplicate keys for one dictionary. ** If there are duplicates, you will overwrite another value.
To add multiple elements to the dictionary, use the ** update method **. Enter the following code in the ** Python Console **. Display the contents of the variable ** foodD ** once and then execute.
>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'carrot', 'd': 'dragon fruits'}
>>> foodD.update({'e' : 'egg', 'f' : 'fried potato'})
>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'carrot', 'd': 'dragon fruits', 'e': 'egg', 'f': 'fried potato'}
It will be in the form of merging another dictionary. If you check ** foodD **, you can see that 'e' and **'f' ** have been added.
Dictionary elements can also be deleted by specifying a key. In fact, like a list, you can delete it using the ** del statement **. Enter the following code in the ** Python console **. Display the contents of the variable ** foodD ** once and then execute.
>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'carrot', 'd': 'dragon fruits', 'e': 'egg', 'f': 'fried potato'}
>>> del foodD['b']
>>> foodD
{'a': 'apple', 'c': 'carrot', 'd': 'dragon fruits', 'e': 'egg', 'f': 'fried potato'}
I was able to confirm that 'b' was deleted.
Also, to remove all elements in the dictionary, use the ** clear method ** as you would for a list.
>>> foodD
{'a': 'apple', 'c': 'carrot', 'd': 'dragon fruits', 'e': 'egg', 'f': 'fried potato'}
>>> foodD.clear()
>>> foodD
{}
You can see that all the elements in the dictionary have been removed and ** {} ** is output, which is empty.
You can also search for elements in the dictionary. This time as well, we will search by specifying the key. In the previous section, the search was performed by specifying the ** variable name [key] **, but if there is no corresponding key, an error will be output. This time, I will explain how to output no error even if there is no key.
Enter the following code in the ** Python console **. First, substitute the dictionary for ** D **.
>>> D = {'NRT' : 'Narita Airport', 'CTS' : 'New Chitose Airport', 'HIJ' : 'Hiroshima Airport'}
>>> D
{'NRT': 'Narita Airport', 'CTS': 'New Chitose Airport', 'HIJ': 'Hiroshima Airport'}
Use the ** get method ** as a way to avoid getting an error even if you don't have the key.
>>> D
{'NRT': 'Narita Airport', 'CTS': 'New Chitose Airport', 'HIJ': 'Hiroshima Airport'}
>>> D.get('NRT')
'Narita Airport'
>>> D.get('MYJ')
First, 'NRT' exists in ** D **, so **'Narita Airport' ** will be returned. 'MYJ' does not exist in ** D **, so nothing is returned.
By the way, if you use the print function to specify a key that does not exist, ** None ** will be returned.
>>> print(D.get('MYJ'))
None
You can also check the existence by using the in operator explained at the time of listing. Unlike the list, specify the key to check.
>>>'NRT' in D
True
>>>'MYJ' in D
False
I've looked through the dictionary, but I think I've confirmed that it's basically the same operation as the list. The difference is that the dictionary specifies the key to the list that specifies the number of the element. It is a data structure that will appear frequently in the future, so let's hold it down.
Recommended Posts