Python> list> append () and extend ()> append: list is added | extend: list elements are added | + = to add list

Operating environment


ideone (Python 3)

@ Scipy Lecture notes, Edition 2015.2 p14

There is an example of using append () and extend () in the list operation.

L = [ 'red', 'blue', 'green', 'black', 'white' ]
L.append('pink')
...
L.extend(['pink', 'purple'])  # extend L, in-place

I've used append () before, but I don't remember extend () very much.

https://docs.python.jp/3/tutorial/datastructures.html 5.1. A little more about list types

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

Try using append () and extend ().

https://ideone.com/CAHncZ

alist = [3, 1, 4]
alist.append([1, 5, 9])
print(alist)
#
blist = [3, 1, 4]
blist.extend([1, 5, 9])
print(blist)

run


[3, 1, 4, [1, 5, 9]]
[3, 1, 4, 1, 5, 9]

Add list by using + =

@shiracamus taught me how to add a list using + =.

Thank you for the information.

Recommended Posts

Python> list> append () and extend ()> append: list is added | extend: list elements are added | + = to add list
[Python] How to use list 3 Added
It's faster to add than to join and extend the list, right?
Difference between append and + = in Python list
How to use is and == in Python
[Introduction to Python] What is the difference between a list and a tuple?
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement
Python list comprehensions that are easy to forget
What are you comparing with Python is and ==?
List of Python code to move and remember
How to remove duplicate elements in Python3 list
Tips for those who are wondering how to use is and == in Python
[Python] Solution to the problem that elements are linked when copying a list
Python> list> extend () or + =
Differences in behavior between append () and "+ =" operators when adding data to a list in Python
[Python] Manipulation of elements in list (array) [Add / Delete]
Add disks to extend LVM SWAP and / home area
[Python] How to sort dict in list and instance in list
How to check in Python if one of the elements of a list is in another list
Make sure all the elements in the list are the same in Python
How to swap elements in an array in Python, and how to reverse an array.
[python] Summary of how to retrieve lists and dictionary elements
Sort and output the elements in the list as elements and multiples in Python.
Useful tricks related to list and for statements in Python
Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
[Python] Convert list to Pandas [Pandas]
[Python] How to use list 1
Python list is not a list
[Python] Python and security-① What is Python?
[Introduction to Python] <list> [edit: 2020/02/22]
Python list and tuples and commas
Python list comprehensions and generators
Getting list elements in Python
Identity and equivalence Python is and ==
[Python] What is pip? Explain the command list and how to use it with actual examples
Memo to switch between python2 series and 3 series in anaconda environment of mac (win is also added)
How to compare lists and retrieve common elements in a list
Regular expressions that are easy and solid to learn in Python
[Python] How to add rows and columns to a table (pandas DataFrame)
How to get a list excluding elements whose index is i ...?
Python a + = b and a = a + b are different
Python 3.6 on Windows ... and to Xamarin.
Difference between list () and [] in Python
Difference between == and is in python
[Introduction to Python3 Day 1] Programming and Python
Add Python 2.7 Japanese documentation to Dash.app
[Python] What are @classmethods and decorators?
Convert list to DataFrame with python
Python> list> Convert double list to single list
Python logging and dump to json
Selenium and python to open google
Add TRACE log level to Python ...?
Delete multiple elements in python list
Combine multiple dictionaries and add multiple elements at once, safely and non-destructively in Python
Challenge to create time axis list report with Toggl API and Python
With PEP8 and PEP257, Python coding that is not embarrassing to show to people!
Migration from Python2 to Python3 (Python2 is rebuilt as a virtual environment and coexists)
UnionFind in python (enhanced version: strings and tuples are allowed for elements)
Extract every n elements from an array (list) in Python and Ruby
I hear that Matlab and Python loops are slow, but is that true?
[Python] How to use list 2 Reference of list value, number of elements, maximum value, minimum value