[Introduction to Python] <list> [edit: 2020/02/22]

table of contents

  1. [Input data setting](# step1-Input data setting)
  2. [Data Manipulation / Algorithm](# step2-Data Manipulation--Algorithm)
  3. [Output data setting](# step3-Output data setting)

Step1 Input data setting

1. Basic operation

1-1. Easy definition method

1D list


sample_list_1d = [1,2,3,4,5]
>>> IN: print("sample_list_1d: ", sample_list_1d)
>>> OUT: sample_list_1d:  [1, 2, 3, 4, 5]

2D list


sample_list_2d = [[1,2,3,4,5],[6,7,8,9,10]]
>>> IN: print("sample_list_2d: ", sample_list_2d)
>>> OUT: sample_list_2d:  [[1,2,3,4,5],[6,7,8,9,10]]

1-2. Element access [number of elements: 1]

1D list


sample_list_1d = [1,2,3,4,5]
>>> IN[0]: print("sample_list_1d[0] = ",sample_list_1d[0])
>>> IN[1]: print("sample_list_1d[1] = ",sample_list_1d[1])
>>> IN[2]: print("sample_list_1d[2] = ",sample_list_1d[2])
>>> IN[3]: print("sample_list_1d[3] = ",sample_list_1d[3])
>>> IN[4]: print("sample_list_1d[4] = ",sample_list_1d[4])
>>> IN[5]: print("sample_list_1d[-1] = ",sample_list_1d[-1])
>>> OUT[0]: sample_list_1d[0] =  1
>>> OUT[1]: sample_list_1d[1] =  2
>>> OUT[2]: sample_list_1d[2] =  3
>>> OUT[3]: sample_list_1d[3] =  4
>>> OUT[4]: sample_list_1d[4] =  5
>>> OUT[5]: sample_list_1d[-1] =  5

2D list


sample_list_2d = [[1,2,3,4,5],[6,7,8,9,10]]
>>> IN[0]: print("sample_list_2d[0][0] = ",sample_list_2d[0][0])
>>> IN[1]: print("sample_list_2d[0][1] = ",sample_list_2d[0][1])
>>> IN[2]: print("sample_list_2d[0][2] = ",sample_list_2d[0][2])
>>> IN[3]: print("sample_list_2d[0][3] = ",sample_list_2d[0][3])
>>> IN[4]: print("sample_list_2d[0][4] = ",sample_list_2d[0][4])
>>> IN[5]: print("sample_list_2d[1][0] = ",sample_list_2d[1][0])
>>> IN[6]: print("sample_list_2d[1][1] = ",sample_list_2d[1][1])
>>> IN[7]: print("sample_list_2d[1][2] = ",sample_list_2d[1][2])
>>> IN[8]: print("sample_list_2d[1][3] = ",sample_list_2d[1][3])
>>> IN[9]: print("sample_list_2d[1][4] = ",sample_list_2d[1][4])
>>> OUT[0]: sample_list_1d[0] =  1
>>> OUT[1]: sample_list_1d[1] =  2
>>> OUT[2]: sample_list_1d[2] =  3
>>> OUT[3]: sample_list_1d[3] =  4
>>> OUT[4]: sample_list_1d[4] =  5
>>> OUT[5]: sample_list_1d[4] =  6
>>> OUT[6]: sample_list_1d[4] =  7
>>> OUT[7]: sample_list_1d[4] =  8
>>> OUT[8]: sample_list_1d[4] =  9
>>> OUT[9]: sample_list_1d[4] =  10

1-3. Element access [Number of elements: Multiple]

1D list


sample_list_1d = [1,2,3,4,5]
start_id = 1
end_id = 3
>>> print("Item numbers "1" to "3":",sample_list_1d[start_id:end_id+1])
>>> print("Item number "First" to "3":",sample_list_1d[:end_id+1])
>>> print("Item number "1" to "last":",sample_list_1d[start_id:])
>>> print("All elements:",sample_list_1d[:])
>>>Item numbers "1" to "3":[2, 3, 4]
>>>Item number "First" to "3":[1, 2, 3, 4]
>>>Item number "1" to "last":[2, 3, 4, 5]
>>>All elements:[1, 2, 3, 4, 5]

1-4. List Length

1D list


sample_list_1d = [1,2,3,4,5]
print("List length:",len(sample_list_1d))
List length: 5

2D list


sample_list_2d = [[1,2,3,4,5],[6,7,8,9,10]]
print("Parent list length:",len(sample_list_2d))         ###Parent list
print("First child list length:",len(sample_list_2d[0]))  ###First child list
Parent list length: 2
First child list length: 5
List length: 5

1-5. Storage type (Data Type)

1D list


sample_list = [1,2,3,4,5]
print("Storage type:",type(sample_list))
Storage type:<class 'list'>

1-6. Element Data Type

1D list


sample_list = [1,2,3,4,5]
print("Element data type:",type(sample_list[0]))
Element data type:<class 'int'>

1-7. Element Append

1D list


sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 3, 4, 5]

sample_list.append(6)
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 3, 4, 5, 6]

1-8. Addition of specified position element [Number of elements: 1]

1D list


sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 3, 4, 5]

specified_id = 2
specified_value = 8
sample_list.insert(specified_id,specified_value)
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 8, 3, 4, 5]

1-9. Add specified position element [Number of elements: Multiple]

1D list


sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 3, 4, 5]

specified_id = 2
specified_value_list = [8,8,8]
sample_list[specified_id:specified_id] = specified_value_list
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 8, 8, 8, 3, 4, 5]

1-10. Delete element [value]

1D list


sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 3, 4, 5]

sample_list.remove(5)
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 3, 4]

1D list[If there are multiple]


sample_list = [1,2,3,1,2,3]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 3, 1, 2, 3]

sample_list.remove(1)
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [2, 3, 1, 2, 3]

1-11. Delete element [Item number] [Method 1]

1D list


sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 3, 4, 5]

del sample_list[2]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 4, 5]

1D list[Multiple]


sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 3, 4, 5]

del sample_list[1:4]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 5]

1-12. Delete element [Item number] [Method 2]

1D list


sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 3, 4, 5]

specified_id = 2
sample_list[specified_id:specified_id+1] = []
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 4, 5]

1D list[Multiple]


sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 3, 4, 5]

specified_start_id = 1
specified_end_id = 3
sample_list[specified_start_id:specified_end_id+1] = []
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 5]

1-13. Delete all elements

1D list


sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  [1, 2, 3, 4, 5]

sample_list.clear()
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  []

1-14. List join (method 1)

1D list


sample_list = ["H","e","l","l","o"]
extend_list = ["W","o","r","l","d"]
sample_list.extend(extend_list)
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']

1-15. List join (method 2)

1D list


sample_list = ["H","e","l","l","o"]
extend_list = ["W","o","r","l","d"]
sample_list = sample_list + extend_list
>>> IN: print("sample list: ",sample_list)
>>> OUT: sample list:  ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']

2. Data definition

2-1. Zero List

1D list


array_length = 10
zero_list = [0] * array_length
>>> IN: print("zero list: ",zero_list)
>>> OUT: zero list:  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

2-2. Specified Value List

1D list


specified_value = 3
array_length = 10
specified_value_list = [specified_value] * array_length
>>> IN: print("specified value list: ",specified_value_list)
>>> OUT: specified value list:  [3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

2-3. Empty List

1D list[Number of elements: 1]


empty_list = []
print("empty list: ",empty_list)
empty list:  []

1D list[Number of elements: Multiple]


array_length = 10
empty_list = [[]] * array_length
print("empty list: ",empty_list)
empty list:  [[], [], [], [], [], [], [], [], [], []]

2-4. Arithmetic Sequence List

1D list


start_num = 1
end_num = 10
difference = 1
arithmetic_sequence_list = list(range(start_num, end_num + 1, difference))
>>> IN: print("arithmetic sequence list: ",arithmetic_sequence_list)
>>> OUT: arithmetic sequence list:  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

3. Basic conversion

3-1. Change element data type

1D list


sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
### sample list:  [1, 2, 3, 4, 5]

sample_list = list(map(str,sample_list))
>>> IN: print("sample list: ",sample_list)
### sample list:  ['1', '2', '3', '4', '5']

3-2. 「string list」to「string」

1D list


sample_list= ["H","e","l","l","o"]
>>> IN: print("sample list: ",sample_list)
### OUT: sample list:  ['H', 'e', 'l', 'l', 'o']

joint_value = ""
sample_string = joint_value.join(sample_list)
>>> IN: print("sample string: ",sample_string)
### OUT: sample string:  Hello

3-3. 「string」to「string list」

1D list


sample_string = "HelloWorld"
>>> IN: print("sample string: ",sample_string)
### sample string:  HelloWorld

sample_list= list(sample_string)
>>> IN: print("sample string: ",sample_string)
### OUT: sample list:  ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']

3-4. 「sentence」to「word list」

1D list


sample_sentence = "Hello World"
>>> IN: print("sample sentence: ",sample_sentence)
### OUT: sample sentence:  Hello World

split_value = " "
sample_list= sample_sentence.split(split_value)
>>> IN: print("sample list: ",sample_list)
### OUT: sample list:  ['Hello', 'World']

4. Storage format conversion

4-1. numpy ndarray

4-1-1. 「list」to「numpy ndarray」

1D list


import numpy as np
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> IN: print("type: ",type(sample_list))sample 
### OUT: list:  [1, 2, 3, 4, 5]
### OUT: type:  <class 'list'>

sample_ndarray = np.array(sample_list)
>>> IN: print("sample ndarray: ",sample_ndarray)
>>> IN: print("type: ",type(sample_ndarray))
### OUT: sample ndarray:  [1 2 3 4 5]
### OUT: type:  <class 'numpy.ndarray'>

4-1-2. 「numpy ndarray」to「list」

1D list


import numpy as np
sample_ndarray = np.array([1,2,3,4,5])
>>> IN: print("sample ndarray: ",sample_ndarray)
>>> IN: print("type: ",type(sample_ndarray))
### OUT: sample ndarray:  [1 2 3 4 5]
### OUT: type:  <class 'numpy.ndarray'>

sample_list = sample_ndarray.tolist()
>>> IN: print("sample list: ",sample_list)
>>> IN: print("type: ",type(sample_list))
### OUT: sample list:  [1, 2, 3, 4, 5]
### OUT: type:  <class 'list'>

4-2. numpy matrix

4-2-1. 「list」to「numpy matrix」

1D list


import numpy as np
sample_list = [1,2,3,4,5]
>>> IN: print("sample list: ",sample_list)
>>> IN: print("type: ",type(sample_list))
### OUT: sample list:  [1, 2, 3, 4, 5]
### OUT: type:  <class 'list'>

sample_matrix = np.matrix(sample_list)
>>> IN: print("sample matrix: ",sample_matrix)
>>> IN: print("type: ",type(sample_matrix))
### OUT: sample matrix:  [[1 2 3 4 5]]
### OUT: type:  <class 'numpy.matrix'>

4-2-2. 「numpy matrix」to「list」

1D list


import numpy as np
sample_matrix = np.matrix([1,2,3,4,5])
>>> IN: print("sample matrix: ",sample_matrix)
>>> IN: print("type: ",type(sample_matrix))
### OUT: sample matrix:  [[1 2 3 4 5]]
### OUT: type:  <class 'numpy.matrix'>

sample_list = sample_matrix.tolist()
>>> IN: print("sample list: ",sample_list)
>>> IN: print("type: ",type(sample_list))
### OUT: sample list:  [[1, 2, 3, 4, 5]]
### OUT: type:  <class 'list'>

5. File input

5-1. Text file (.txt)

5-2. Tabular file (.csv)

5-3. Image file (.png)

5-4. Object file (.obj)

Step2 Data manipulation / algorithm

1. Search

1-1. Maximum value

1D list


sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
print("Maximum Value: ",max(sample_list))
sample list:  [1, 2, 3, 4, 5]
Maximum Value:  5

1-2. Maximum element number

1D list


sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list:  [1, 2, 3, 4, 5]

max_value_id = [i for i, v in enumerate(sample_list) if v == max(sample_list)]
print("Maximum Value ID: ",max_value_id)
Maximum Value ID:  [4]

1D list[If there are multiple]


sample_list = [1,2,3,1,2,3]
print("sample list: ",sample_list)
sample list:  [1, 2, 3, 1, 2, 3]

max_value_id = [i for i, v in enumerate(sample_list) if v == max(sample_list)]
print("Maximum Value ID: ",max_value_id)
Maximum Value ID:  [2, 5]

1-3. Minimum value

1D list


sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
print("Minimum Value: ",min(sample_list))
sample list:  [1, 2, 3, 4, 5]
Minimum Value:  1

1-4. Minimum element number

1D list


sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list:  [1, 2, 3, 4, 5]

min_value_id = [i for i, v in enumerate(sample_list) if v == min(sample_list)]
print("Minimum Value ID: ",min_value_id)
Minimum Value ID:  [0]

1D list[If there are multiple]


sample_list = [1,2,3,1,2,3]
print("sample list: ",sample_list)
sample list:  [1, 2, 3, 1, 2, 3]

min_value_id = [i for i, v in enumerate(sample_list) if v == min(sample_list)]
print("Minimum Value ID: ",min_value_id)
Minimum Value ID:  [0, 3]

1-5. Top value [in descending order]

1D list


import heapq

sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list:  [1, 2, 3, 4, 5]

print("Top Large Value: ",heapq.nlargest(3, sample_list))
Top Large Value:  [5, 4, 3]

1-6. Top value [smallest first]

1D list


import heapq

sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list:  [1, 2, 3, 4, 5]

print("Top Small Value: ",heapq.nsmallest(3, sample_list))
Top Small Value:  [1, 2, 3]

1-7. Presence or absence of specific value

1D list


sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list:  [1, 2, 3, 4, 5]

print("Have value '1': ",1 in sample_list)
print("Have value '6': ",6 in sample_list)
Have value '1':  True
Have value '6':  False

1-8. Specific value element number

1D list


sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list:  [1, 2, 3, 4, 5]

print("Search ID for value '1': ",sample_list.index(1))
Search ID for value '1':  0

1-9. Specific value count

1D list


sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample list:  [1, 2, 3, 3, 3, 3, 3, 4, 5]

print("Count value '3': ",sample_list.count(3))
Count value '3':  5

2. Reorder / Sort

2-1. Reverse order

1D list


sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sample_list.reverse()
print("reversed list: ",sample_list)

2-2. Sort [Ascending]

1D list


sample_list = [5,4,3,2,1]
print("sample list: ",sample_list)
sorted_list = sorted(sample_list)
print("sorted list: ",sorted_list)

2-3. Sort [Descending]

1D list


sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sorted_list = sorted(sample_list,reverse=True)
print("sorted list: ",sorted_list)

2-4. Sort [Ascending] [Absolute value]

1D list


sample_list = [5,-4,3,-2,1]
print("sample list: ",sample_list)
sorted_list = sorted(sample_list,key=abs)
print("sorted list [abs]: ",sorted_list)

2-5. Sort [Ascending] [String Length]

1D list


sample_list = ["zero","one","two","three","four","five"]
print("sample list: ",sample_list)
sorted_list = sorted(sample_list,key=len)
print("sorted list [string length]: ",sorted_list)

3. List calculation

3-1. Addition

1D list


A = [4,5,6]
B = [1,2,3]
C = [x + y for (x, y) in zip(A, B)]   
print("A: ",A)
print("B: ",B) 
print("add operation: ",C)

3-2. Subtraction

1D list


A = [4,5,6]
B = [1,2,3]
C = [x - y for (x, y) in zip(A, B)]    
print("A: ",A)
print("B: ",B) 
print("substract operation: ",C)

3-3. Multiplication

1D list


A = [4,5,6]
B = [1,2,3]
C = [x - y for (x, y) in zip(A, B)]    
print("A: ",A)
print("B: ",B) 
print("multiply operation: ",C)

3-4. Division

1D list


A = [4,5,6]
B = [1,2,3]
C = [x / y for (x, y) in zip(A, B)]    
print("A: ",A)
print("B: ",B) 
print("divide operation: ",C)

3-5. Remaining

1D list


A = [10,10,10]
B = [1,2,3]
C = [x % y for (x, y) in zip(A, B)]    
print("A: ",A)
print("B: ",B) 
print("mod operation: ",C)

3-6. Sum

1D list


sample_list = [1,2,3,4,5]
print("sample list: ",sample_list)
sum_value = sum(sample_list)
print("Sum Value: ",sum_value)

Step3 Output data setting

1. File output

1-1. Text file (.txt)

1-2. Tabular file (.csv)

1-3. Image file (.png)

1-4. Object file (.obj)

Recommended Posts

[Introduction to Python] <list> [edit: 2020/02/22]
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Introduction to Udemy Python3 + Application] 18. List methods
[Introduction to Udemy Python3 + Application] 17. List operation
Introduction to OpenCV (python)-(2)
[Introduction to Udemy Python3 + Application] 16. List type
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
[Introduction to Udemy Python 3 + Application] 19. Copy of list
Introduction to Python Django (2) Win
[Python] Convert list to Pandas [Pandas]
[Python] How to use list 1
Introduction to serial communication [Python]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
[Python] list
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Udemy Python 3 + Application] 57. Decorator
Introduction to Python Hands On Part 1
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python] How to parse JSON
[Introduction to Udemy Python 3 + Application] 56. Closure
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
Python> list> Convert double list to single list
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python] Let's use pandas
[Python] How to use list 3 Added
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
Introduction to image analysis opencv python
[Introduction to Python] Let's use pandas
An introduction to Python for non-engineers
Introduction to Python Django (2) Mac Edition
[AWS SAM] Introduction to Python version
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Python Tutorial] An Easy Introduction to Python
Introduction to Scrapy (1)
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
Introduction to Scrapy (3)
Updated to Python 2.7.9
[Introduction to Python] How to use class in Python?
Introduction to Supervisor
Python basics: list
Introduction of Python
Introduction to Tkinter 1: Introduction
[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
[Introduction to Udemy Python3 + Application] 33. if statement
Introduction to Discrete Event Simulation Using Python # 1
[Introduction to Udemy Python3 + Application] 13. Character method
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Python] How to convert a 2D list to a 1D list
[Introduction to Udemy Python3 + Application] 55. In-function functions
[Introduction to Udemy Python3 + Application] 48. Function definition
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
A super introduction to Python bit operations