LoginSignup
3
3

More than 5 years have passed since last update.

Python > list > append()とextend() > append:リストが追加される | extend: リストの要素が追加される | +=でのリスト追加

Last updated at Posted at 2017-09-05
動作環境
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 さんに+=を使ったリスト追加方法を教えていただきました。

情報感謝です。

3
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3