[Python] Chapter 04-01 Various data structures (list creation and element retrieval)

[Python] Chapter 04-01 Creating Lists and Extracting Elements

In this chapter, we will describe the data structures handled by Python programs. Among them, I would like to touch on the list this time.

What is an object?

Python is said to be an object-oriented language. Actually, the character string, which was dealt with in Chapter 2, is also an object, but I think that it acted (method) on the thing (object) called the character string.

The list handled this time is also one of the objects. You will also be able to work with such objects.

However, objects are difficult to handle theoretically, so I'll tell you more about them when I talk about object-oriented programming.

Creating a list

Creating a number list

Python allows you to combine relevant data into one. This is called ** list ***.

Enter the following code from the ** Python Console **.

>>>L = [10, 20, 30, 40, 50]

As mentioned above, ** list is enclosed in [] and separated by "," **. A list is assigned to the variable L. You can see the contents of the list with the print function as shown below.

>>>print(L)
[10, 20, 30, 40, 50]

I talked about "variables" first, but I said that values are tagged with variable names. Again, the list is still tagged. image.png

Checking list elements

Each value is called a ** list element **. For example, to retrieve one element, write: This time as well, the ** element numbers in the list start from 0th **, as in the case of strings. Also, the element number is the same as for a character string, and you can also assign a ** minus number **.

Number (1) 0 1 2 3 4
Number (2) -5 -4 -3 -2 -1
Numbers in the list 10 20 30 40 50
>>>L[2]
30
>>>L[-2]
40

The image looks like this: image.png

Of course, if you specify a number that does not exist in the element, an error will occur as shown below.

>>>L[5]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
IndexError: list index out of range

List of strings

Not only numbers but also strings can be assigned to the list. However, if it is a character string, it must be enclosed in "'" (single quotation marks) or "" "(double quotation marks).

Enter the following code from the ** Python Console **.

>>>strL = ['Japan', 'China', 'Korea']
>>>print(strL)
['Japan', 'China', 'Korea']
>>>strL[1]
'China'

A list of character strings is created in a variable called strL, and the elements are specified and output. This is the same as the list of numbers above.

Extract list

I explained slicing in Chapter 02-05. You can also apply slices in a list.

Enter the following code from the ** Python Console **. Assign the list to the variable name ls and output the contents of the variable to check.

>>>ls = ['Japan', 'Canada', 'Australia', 'England', 'German', 'Italy', 'France']
>>>print(ls)
['Japan', 'Canada', 'Australia', 'England', 'German', 'Italy', 'France']

The contents of the variable ls are summarized below with element numbers.

Number (1) 0 1 2 3 4 5 6
Number (2) -7 -6 -5 -4 -3 -2 -1
String in the list 'Japan' 'Canada' 'Australia' 'England' 'German' 'Italy' 'France'

Based on the above, I will explain using slices.

>>>ls[2:]
['Australia', 'England', 'German', 'Italy', 'France']
>>>ls[:5]
['Japan', 'Canada', 'Australia', 'England', 'German']
>>>ls[2:5]
['Australia', 'England', 'German']
>>>ls[:-3]
['Japan', 'Canada', 'Australia', 'England']
>>>ls[1:-3]
['Canada', 'Australia', 'England']

First, I would like to consider the following results.

>>>ls[2:]
>>>ls[:5]
>>>ls[2:5]

As explained in the string slice in Chapter 02-05, it may be easier to understand if you think mathematically. For ** ls [2:] **, it is as follows. (Think of x as a number)

2≦x

In other words, all values with element numbers 2 or higher in the list will be output. ** ls [: 3] ** and ** ls [2: 5] ** are also as follows. The treatment is almost the same as when it is a character string.

x<5\\
2≦x<5

Next, I would like to look at the following slices.

>>>ls[:-3]
>>>ls[1:-3]

I think this is easy to understand if you combine it with the table above. The element "-3" is'German'in the table above, but this time it does not include the element number of -3, that is

x<-3

Therefore, in the above table, "-7" "-6" "-5" "-4" (that is, ** ['Japan','Canada','Australia','England'] **) Will be applicable.

** ls [1: -3] ** may also be easier to understand by comparing it with the table.

Finally

This time I mentioned the list in the data structure. You will definitely need the list when you analyze data in Python in the future, so be sure to keep it down. Next time, I would like to talk about working with lists.

Return to [Table of Contents Link]

Recommended Posts

[Python] Chapter 04-01 Various data structures (list creation and element retrieval)
[Python] Chapter 04-05 Various data structures (tuple creation and features)
[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] [Supplement] Chapter 04-08 Various data structures (creating and manipulating sets)
[Python] Chapter 04-06 Various data structures (creating dictionaries)
[Python] Chapter 04-07 Various data structures (dictionary manipulation)
[Python] [Supplement] Chapter 04-09 Various data structures (set theory and operations in sets)
Python data structure and internal implementation ~ List ~
List of Python libraries for data scientists and data engineers
Python list and tuples and commas
Python for Data Analysis Chapter 2
Python list comprehensions and generators
Python for Data Analysis Chapter 3
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
Memo "Chapter 5 – Dictionaries and Structuring Data"
Python data structures learned with chemoinformatics
Hashing data in R and Python
Data pipeline construction with Python and Luigi
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 comprehension (list and generator expressions) [additional]
Difference between append and + = in Python 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 data structure and operation (Python learning memo ③)
[Python] Various data processing using Numpy arrays
Easily graph data in shell and Python
[Python Iroha] Difference between List and Tuple
Compress python data and write to sqlite
[Python] Various combinations of strings and values
Exchange encrypted data between Python and C #
[Python] Precautions when retrieving data by scraping and putting it in the list