[Python] Chapter 04-06 Various data structures (creating dictionaries)

[Python] Chapter 04-06 Creating a dictionary

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.

Creating a dictionary

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.

Reference of dictionary elements

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.

Finally

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.

Return to [Table of Contents Link]

Recommended Posts

[Python] Chapter 04-06 Various data structures (creating dictionaries)
[Python] Chapter 04-03 Various data structures (multidimensional list)
[Python] Chapter 04-04 Various data structures (see list)
[Python] Chapter 04-02 Various data structures (list manipulation)
[Python] Chapter 04-07 Various data structures (dictionary manipulation)
[Python] [Supplement] Chapter 04-08 Various data structures (creating and manipulating sets)
[Python] Chapter 04-05 Various data structures (tuple creation and features)
[Python] Chapter 04-01 Various data structures (list creation and element retrieval)
[Python] [Supplement] Chapter 04-09 Various data structures (set theory and operations in sets)
Python for Data Analysis Chapter 4
Python for Data Analysis Chapter 2
Python for Data Analysis Chapter 3
Memo "Chapter 5 – Dictionaries and Structuring Data"
Python data structures learned with chemoinformatics
[Python] Chapter 03-01 turtle graphics (creating a turtle)
Python Application: Data Visualization Part 3: Various Graphs
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
Ant book in python: Sec. 2-4, data structures
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
[Python] Various data processing using Numpy arrays
Creating Google Spreadsheet using Python / Google Data API
Creating training data
Data analysis python
# 3 [python3] Various operators
[Python for Hikari] Chapter 09-02 Classes (Creating and instantiating classes)
Python Application: Data Handling Part 2: Parsing Various Data Formats
[python] Read data
[Grasshopper] When creating a data tree on Python script
Started creating the Python aircraft development library IAEA. -Chapter 1-
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
Data analysis with python 2
Python Data Visualization Libraries
Data analysis using Python 0
Data analysis overview python
Python3 | Lists, Tuples, Dictionaries
Various Python visualization tools
Data cleaning using Python
Python lists, tuples, dictionaries
Python data analysis template
[Python tutorial] Data structure
[Python] Sorting Numpy data
[Python] Chapter 01-01 About Python (First Python)
Data analysis with Python
Various processing of Python
I tried to make various "dummy data" with Python faker
Various ways to calculate the similarity between data in python
Memo "Automate the Boring stuff -chapter5 Dictionaries and Structuring Data"