In this section, we will discuss dictionaries. It's the same that a variable has several elements, such as a list or tuple. Dictionaries also allow you to associate values for specific items.
Here, the expression ** "dictionary" ** is used, but some Python books may simply be ** "dictionary" **, so please be aware of this.
A dictionary is a data structure in which each element has a pair of ** key ** and ** value **.
Let's create a dictionary right away. Enter the following code from ** Python Console **. In this example, I would like to explain using the abbreviation name of the Information-Technology Engineers Examination.
>>> D = {'ip' : 'IT passport exam', 'fe' : 'Basic Information Technology Engineer Examination', 'sg' : 'Information Security Management Exam', 'ap' : 'Applied Information Technology Engineer Examination'}
>>> D
{'ip': 'IT passport exam', 'fe': 'Basic Information Technology Engineer Examination', 'sg': 'Information Security Management Exam', 'ap': 'Applied Information Technology Engineer Examination'}
If you look at it like this, you can see what ** ip ** is like in an actual dictionary.
In the dictionary, the key and value are separated by: (colon) and each element is separated by, (comma), such as ** {key 1: value 1, key 2: value 2,…} **. Then enclose them in ** {} **.
It is also possible to extract only the key, extract only the value, and so on. Check the ** D ** element once and then change it.
>>> D = {'ip' : 'IT passport exam', 'fe' : 'Basic Information Technology Engineer Examination', 'sg' : 'Information Security Management Exam', 'ap' : 'Applied Information Technology Engineer Examination'}
>>> D.keys()
dict_keys(['ip', 'fe', 'sg', 'ap'])
>>> D.values()
dict_values(['IT passport exam', 'Basic Information Technology Engineer Examination', 'Information Security Management Exam', 'Applied Information Technology Engineer Examination'])
Use the ** keys () method ** if you want to know the keys, or the ** values method ** if you want to know the values.
Element references in dictionaries are a bit special. In lists and tuples, the number of each element was specified as ** [] ** and displayed, but if you do that in the dictionary, an error will occur. The dictionary also uses ** [] ** when referencing an element, but the method of specifying it is different.
Enter the following code from ** Python Console **. In the dictionary, ** specify the key ** to display the elements as shown below.
>>> D['fe']
'Basic Information Technology Engineer Examination'
Of course, if you specify a key that does not exist, an error will occur.
>>> D['db']
Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 'db'
To change the value of the element, you can specify the key and rewrite it. Check the ** D ** element once and then change it.
>>> D
{'ip': 'IT passport exam', 'fe': 'Basic Information Technology Engineer Examination', 'sg': 'Information Security Management Exam', 'ap': 'Applied Information Technology Engineer Examination'}
>>> D['ap'] = 'Software development engineer exam'
>>> D
{'ip': 'IT passport exam', 'fe': 'Basic Information Technology Engineer Examination', 'sg': 'Information Security Management Exam', 'ap': 'Software development engineer exam'}
This variable ** D ** will be used again, so restore **'ap' **.
>>> D['ap'] = 'Applied Information Technology Engineer Examination'
>>> D
{'ip': 'IT passport exam', 'fe': 'Basic Information Technology Engineer Examination', 'sg': 'Information Security Management Exam', 'ap': 'Applied Information Technology Engineer Examination'}
I know that the value can be changed, but the key cannot be changed. As I mentioned in the tuple earlier, the key is immutable and cannot be changed.
This time I touched on the dictionary. You can find key / value combinations everywhere in computer-related books. Familiarize yourself with key and value manipulation.
Recommended Posts