List concatenation method in python, difference between list.extend () and “+” operator

How to concatenate Python lists

It ’s very rudimentary, I was addicted to python list concatenation, so I will summarize it.

There are two ways to connect.

  1. Use the list.extend () method
  2. Concatenate list objects with the + operator

listSample.py



hoge = ["1", "2"]
foo  = ["3", "4"]

#NG that tends to be done: hoge.extend(foo)The return value of is None. Therefore NoneType has no len.
while len(hoge) < 10:
    hoge = hoge.extend(foo)

# OK
while len(hoge) < 10:
    hoge = hoge + foo

# OK
while len(hoge) < 10:
    hoge.extend(foo)

References

Speed comparison for leisurely Python list addition (append, comprehension, etc.)

Recommended Posts

List concatenation method in python, difference between list.extend () and “+” operator
Difference between list () and [] in Python
Difference between append and + = in Python list
Difference between == and is in python
[Python] Difference between function and method
difference between statements (statements) and expressions (expressions) in Python
Difference between @classmethod and @staticmethod in Python
Difference between nonlocal and global in Python
[Python] Difference between class method and static method
[Python Iroha] Difference between List and Tuple
[python] Difference between variables and self. Variables in class
About the difference between "==" and "is" in python
Difference between return, return None, and no return description in Python
Difference in writing method to read external source code between Ruby and Python
Difference between Ruby and Python split
List method argument information for classes and modules in Python
Python module num2words Difference in behavior between English and Russian
Difference between python2 series and python3 series dict.keys ()
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
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)
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
Differences in syntax between Python and Java
Difference between PHP and Python finally and exit
[python] Difference between rand and randn output
Differences in multithreading between Python and Jython
Difference between numpy.ndarray and list (dimension, size)
[Introduction to Python] What is the difference between a list and a tuple?
Sorted list in Python
Simplex method (simplex method) in Python
List find in Python
Private method in python
Implemented List and Bool in Python and SQLite3 (personal note)
[python] Calculation of months and years of difference in datetime
[Python] How to sort dict in list and instance in list
Mutual conversion between JSON and YAML / TOML in Python
Compare "relationship between log and infinity" in Gauche (0.9.4) and Python (3.5.1)
Differences in behavior between append () and "+ =" operators when adding data to a list in Python
[Python] Explain the difference between strftime and strptime in the datetime module with an example
[Memo] Difference between test_split and cross-validation method when verifying generalized performance in deep learning
Install OpenCV 3 (core + contrib) in Windows & Python 3 environment & Difference between OpenCV 2 and OpenCV 3 & Easy operation check
Difference between process and job
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Find the difference in Python
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
Sort and output the elements in the list as elements and multiples in Python.
Difference between regression and classification
[Python] How to delete rows and columns in a table (list of drop method options)
Useful tricks related to list and for statements in Python
Convenient writing method when appending to list continuously in Python
Stack and Queue in Python
Get the current date and time in Python, considering the time difference
Implement method chain in Python
Python bitwise operator and OR
Python list and tuples and commas
Python list comprehensions and generators
Difference between np.array and np.arange