Difference between append and + = in Python list

I'm addicted to handling CSV in Python, so make a note.

List join operation

Suppose that the CSV of the gradebook is read by the standard library and the following list is created. Report card for 2 people. Math and English scores are recorded respectively.

paper1 = [
    ["Math", "90"],
    ["English", "75"]
]
paper2 = [
    ["Math", "38"],
    ["English", "100"]
]

At this time, which of append and + = should be used to create a list that manages a bundle of gradebooks?

First + =

Let's take a look at REPL for the time being.

>>> papers = []
>>> papers += paper1
>>> papers += paper2
>>> papers
[['Math', '90'], ['English', '75'], ['Math', '38'], ['English', '100']]

Oops? Have you been merged? This is not the expected result.

Next is append

This is also a REPL.

papers = []
>>> papers.append(paper1)
>>> papers.append(paper2)
>>> papers
[[['Math', '90'], ['English', '75']], [['Math', '38'], ['English', '100']]]

Two gradebooks are included separately. This is the result I expected.

Seed testimony

In conclusion, the + and + = operators called the extend () method. You were expanding the list of places to put it. Lists are combined.

On the other hand, the append () method appends without joining even if the object to be inserted is a list.

References https://note.nkmk.me/python-list-append-extend-insert/

Recommended Posts

Difference between append and + = in Python list
Difference between list () and [] in Python
Difference between == and is in python
difference between statements (statements) and expressions (expressions) in Python
Difference between @classmethod and @staticmethod in Python
Difference between nonlocal and global in Python
[Python Iroha] Difference between List and Tuple
List concatenation method in python, difference between list.extend () and β€œ+” operator
[python] Difference between variables and self. Variables in class
About the difference between "==" and "is" in python
Difference between Ruby and Python in terms of variables
Difference between return, return None, and no return description in Python
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between python2 series and python3 series dict.keys ()
[Python] Difference between function and method
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
Python module num2words Difference in behavior between English and Russian
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
Differences in behavior between append () and "+ =" operators when adding data to a list in Python
Differences in syntax between Python and Java
Difference between PHP and Python finally and exit
[Python] Difference between class method and static method
[python] Difference between rand and randn output
Differences in multithreading between Python and Jython
Difference between numpy.ndarray and list (dimension, size)
Difference in how to write if statement between ruby ​​and python
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python
File open function in Python3 (difference between open and codecs.open and speed comparison)
Sorted list in Python
Filter List in Python
List find in Python
[Introduction to Python] What is the difference between a list and a tuple?
Difference between process and job
Implemented List and Bool in Python and SQLite3 (personal note)
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Find the difference in Python
Difference between regression and classification
[python] Calculation of months and years of difference in datetime
[Python] How to sort dict in list and instance in list
Stack and Queue in Python
Mutual conversion between JSON and YAML / TOML in Python
Python list and tuples and commas
Python list comprehensions and generators
Difference between np.array and np.arange
Difference between MicroPython and CPython
Unittest and CI in Python
Difference in writing method to read external source code between Ruby and Python
Getting list elements in Python
Compare "relationship between log and infinity" in Gauche (0.9.4) and Python (3.5.1)
Difference between ps a and ps -a
Difference between return and print-Python
[Python] Explain the difference between strftime and strptime in the datetime module with an example
Install OpenCV 3 (core + contrib) in Windows & Python 3 environment & Difference between OpenCV 2 and OpenCV 3 & Easy operation check
List method argument information for classes and modules in Python
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
Useful tricks related to list and for statements in Python
Get the current date and time in Python, considering the time difference