[Python] Chapter 04-03 Various data structures (multidimensional list)

[Python] Chapter 04-03 Multidimensional List

Until the last time, I learned how to create a list and operate from it. The list I created so far was a one-dimensional list. This time, we will look at creating a list of two or more dimensions.

Creating a multidimensional list

The list I've learned so far has been a one-dimensional list. You could put strings and numbers in this list, and you could put any data type.

In fact, you can put more lists in the list. This is called a ** multidimensional list **.

Let's look at it concretely. Enter the following code in the ** Python Console **.

>>>lsls = [[23, 24], [37, 38], [41, 42]]
>>>lsls
[[23, 24], [37, 38], [41, 42]]

Each element of the list is a list. This is represented in a table as follows.

number 0 1 2
element [23, 24] [37, 38] [41, 42]

You can check each element by specifying the element number in ** lls ** while referring to the above table.

>>>lsls[2]
[41, 42]

So how do you get 42 out of [41, 42]? 42 in [41, 42] is the first element in the second element, so specify as follows.

>>>lsls
[[23, 24], [37, 38], [41, 42]]
>>>lsls[2][1]
42

In addition, I would like you to consider it as an introduction, but you can also create a 3D list as shown below.

>>>lslsls = [[[1, 2],[5, 6]], [[10, 11],[15, 16]], [[26, 27],[28, 29]]]
>>>lslsls
[[[1, 2], [5, 6]], [[10, 11], [15, 16]], [[26, 27], [28, 29]]]
>>>lslsls[2]
[[26, 27], [28, 29]]
>>>lslsls[2][0]
[26, 27]
>>>lslsls[2][0][1]
27

The table is as follows. You can see how the two-dimensional list is stored in the elements of the list.

number 0 1 2
element [[1, 2],[5, 6]] [[10, 11],[15, 16]] [[26, 27],[28, 29]]

Finally

The last 3D list is complicated, so up to a 2D list is fine. The actual data is often represented in tabular form. Keep in mind that you can also store such data in a two-dimensional list for processing.

Return to [Table of Contents Link]

Recommended Posts

[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] Chapter 04-01 Various data structures (list creation and element retrieval)
[Python] Chapter 04-05 Various data structures (tuple creation and features)
[Python] [Supplement] Chapter 04-08 Various data structures (creating and manipulating sets)
[Python] [Supplement] Chapter 04-09 Various data structures (set theory and operations in sets)
Python for Data Analysis Chapter 4
[Python] Copy of multidimensional list
Python for Data Analysis Chapter 2
Python for Data Analysis Chapter 3
Python data structures learned with chemoinformatics
[Python] list
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)
Python data structure and internal implementation ~ List ~
[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
[Python] List Comprehension Various ways to create a list
Python basics: list
[python] Create a list of various character types
Data analysis python
Python multidimensional array
# 3 [python3] Various operators
Python> Comprehension / Comprehension> List comprehension
Python Application: Data Handling Part 2: Parsing Various Data Formats
Python list manipulation
[python] Read data
List of Python libraries for data scientists and data engineers
List of Python code used in big data analysis
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
Sorted list in Python
Python Exercise 2 --List Comprehension
Data analysis with python 2
List of python modules
Python> list> extend () or + =
Sorted list in Python
Data analysis using Python 0
Data analysis overview python
Filter List in Python
python unittest assertXXX list
Various Python visualization tools
Data cleaning using Python
Python3 List / dictionary memo
[Memo] Python3 list sort
OpenCV3 Python API list
Python error list (Japanese)
List find in Python
Python data analysis template
[Python tutorial] Data structure
[Python] Sorting Numpy data
[Python] Chapter 01-01 About Python (First Python)
Data analysis with Python
Python exception class list
Various processing of Python
Initialize list with python
I tried to make various "dummy data" with Python faker