Differences in behavior between append () and "+ =" operators when adding data to a list in Python

Note that I confirmed the difference in the behavior of the ʻappend ()and+ =` operators when adding data to a list in Python.

In the case of ʻappend ()`

Regardless of the type (character string type, numeric type, dictionary type, array type, etc.), the specified data is added to the array as one element.

>>> d_list = []
>>> d_list.append('Hello')
>>> d_list.append(123)
>>> d_list.append({'a':1, 'b':2, 'c':3})
>>> d_list.append([1, 2, 3])
>>> d_list
['Hello', 123, {'a': 1, 'b': 2, 'c': 3}, [1, 2, 3]]

When + =

It acts as an iterator and adds all the iterable elements of the specified data to the array. The operation is the same as ʻextend ()`.

Each character is added as one element in the character string type data.

>>> d_list = []
>>> d_list += 'Hello'
>>> d_list
['H', 'e', 'l', 'l', 'o']

Numeric type data is not iterable, so an error will occur.

>>> d_list += 123
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

In dictionary type data, the key name of each element is added as one element.

>>> d_list = []
>>> d_list += {'a':1, 'b':2, 'c':3}
>>> d_list
['a', 'b', 'c']

For array type data, each element is added as one element.

>>> d_list = [1, 2]
>>> d_list += [3, 4, 5]
>>> d_list
[1, 2, 3, 4, 5]

reference

5.1. A little more about list types

list.append(x) Add an element to the end of the list. Equivalent to a [len (a):] = [x].

list.extend(iterable) Extend the list by adding all the elements of the iterable to the target list. Equivalent to a [len (a):] = iterable.

that's all

Recommended Posts

Differences in behavior between append () and "+ =" operators when adding data to a list in Python
Difference between append and + = in Python list
Things to note when initializing a list in Python
Difference between list () and [] in Python
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
Differences in syntax between Python and Java
Differences in multithreading between Python and Jython
[Introduction to Python] What is the difference between a list and a tuple?
How to clear tuples in a list (Python)
[Python] Precautions when retrieving data by scraping and putting it in the list
[Python] How to sort dict in list and instance in list
Python module num2words Difference in behavior between English and Russian
Behavior when giving a list with shell = True in subprocess
Various ways to calculate the similarity between data in python
List concatenation method in python, difference between list.extend () and “+” operator
Developed a library to get Kindle collection list in Python
A standard way to develop and distribute packages in Python
Useful tricks related to list and for statements in Python
Build a Python environment and transfer data to the server
Convenient writing method when appending to list continuously in Python
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
I tried to enumerate the differences between java and python
Just try to receive a webhook in ngrok and python
[Python] How to delete rows and columns in a table (list of drop method options)
Recursively get the Excel list in a specific folder with python and write it to Excel.
When I got a list of study sessions in Python, I found something I wanted to make
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
processing to use notMNIST data in Python (and tried to classify it)
Difference between == and is in python
Write a script in Shell and Python to notify you in Slack when the process is finished
Python> list> append () and extend ()> append: list is added | extend: list elements are added | + = to add list
How to compare lists and retrieve common elements in a list
Try to get a list of breaking news threads in Python.
Behavior when listing in Python heapq
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Timezone specification when converting a string to datetime type in python
Difference in how to write if statement between ruby ​​and python
Differences between Python, stftime and strptime
Precautions when passing def to sorted and groupby functions in Python? ??
Hashing data in R and Python
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
A story about everything from data collection to AI development and Web application release in Python (3. AI development)
How to delete multiple specified positions (indexes) in a Python list
When creating a matrix in a list
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
When writing a program in Python
Write a C language linked list in an object-oriented style (with code comparison between Python and Java)
How to send a visualization image of data created in Python to Typetalk
[Python] How to put any number of standard inputs in a list
How to put a half-width space before letters and numbers in Python.
Data analysis: Easily apply descriptive and inference statistics to CSV data in Python
Connect to postgreSQL from Python and use stored procedures in a loop.
How to write a string when there are multiple lines in python
How to format a list of dictionaries (or instances) well in Python
What to do when a warning message is displayed in pip list
How to stop a program in python until a specific date and time
difference between statements (statements) and expressions (expressions) in Python
Precautions when pickling a function in python