動作環境
ideone (Python 3)
@ Scipy Lecture notes, Edition 2015.2
p14
list操作にappend()とextend()の使用例がある。
L = [ 'red', 'blue', 'green', 'black', 'white' ]
L.append('pink')
...
L.extend(['pink', 'purple']) # extend L, in-place
append()は使ってきたことがあったが、extend()はあまり覚えていない。
https://docs.python.jp/3/tutorial/datastructures.html
5.1. リスト型についてもう少し
list.extend(iterable)
イテラブルのすべての要素を対象のリストに追加し、リストを拡張します。a[len(a):] = iterable と等価です。
append()とextend()を使ってみる。
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]
+=
使用でのリスト追加
@shiracamus さんに+=
を使ったリスト追加方法を教えていただきました。
情報感謝です。