DEV Community

loading...

Python リスト型 list

maru3kaku4kaku profile image maru3kaku4kaku Originally published at marusankakusikaku.jp ・2 min read

Pythonのリスト型listは基本的なシーケンス型の1つでミュータブル(作成後に変更できるオブジェクト)です。

リストの定義

コンマ区切で要素を並べ、角括弧で囲む他、
内包表記やlistを使って定義できます。

list_sample =  []
list_sample =  ['a', 'b', 'c']
list_sample =  [x for x in 'xyz']
list_sample =  list([1, 2, 3])
Enter fullscreen mode Exit fullscreen mode

リストの入れ子

m = [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]
print(m[1]) # => ['D', 'E', 'F']]
print(m[1][2]) # =>  F
Enter fullscreen mode Exit fullscreen mode

リスト情報の取得

リストの要素へのアクセス

インデックスやスライスでのアクセスが可能です。

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample[0] # => a
list_sample[1] # => b
list_sample[-1] # => j
list_sample[2:5] # => ['c', 'd', 'e']
list_sample[3:9:2] # => ['d', 'f', 'h']
list_sample[:] # => ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
Enter fullscreen mode Exit fullscreen mode

リストの長さ

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
print(len(list_sample)) # => 10
Enter fullscreen mode Exit fullscreen mode

回数カウント

list_sample =  ['a', 'b', 'a', 'c', 'a', 'c']
list_sample.count('a') # => 3
Enter fullscreen mode Exit fullscreen mode

要素の存在判定

list_sample =  ['a', 'b', 'c']
'a' in list_sample # => True
'X' not in list_sample # => True
Enter fullscreen mode Exit fullscreen mode

最大最小

list_sample =  [1, 5, 2, 4]
max(list_sample) # => 5
min(list_sample) # => 1
Enter fullscreen mode Exit fullscreen mode

インデックスの位置

index(対象の要素[, 開始位置[, 終了位置]])で要素の位置を取得

list_sample =  ['a', 'b', 'a', 'b', 'c', 'a' , 'c']
list_sample.index('a')  # => 0
list_sample.index('a', 1)  # => 2
list_sample.index('a', 3 , 6)  # => 5
Enter fullscreen mode Exit fullscreen mode

リストの操作

リスト要素の更新

list_sample =  ['a', 'b', 'c']
list_sample[1] = 'X'
list_sample # => ['a', 'X', 'c']
Enter fullscreen mode Exit fullscreen mode

要素の追加

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample.append('X')
print(list_sample) # => ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'X']
Enter fullscreen mode Exit fullscreen mode

要素の挿入

list_sample =  ['a', 'b', 'c']
list_sample.insert(2,'X')
list_sample # => ['a', 'b', 'X', 'c']
Enter fullscreen mode Exit fullscreen mode

指定した値と同じ最初の要素を除去

list_sample =  ['a', 'b', 'c', 'b', 'a']
list_sample.remove('b')
list_sample # => ['a', 'c', 'b', 'a']
Enter fullscreen mode Exit fullscreen mode

指定位置の要素を取り出す

list_sample =  ['a', 'b', 'c']
list_sample.pop(1) # => 'b'
list_sample # => ['a', 'c']
Enter fullscreen mode Exit fullscreen mode

delによる削除

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
del list_sample[3:5]
list_sample # => ['a', 'b', 'c', 'f', 'g', 'h', 'i', 'j']
del list_sample[2:7:2]
list_sample # => ['a', 'b', 'f', 'h', 'j']
Enter fullscreen mode Exit fullscreen mode

要素のクリア

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample.clear()
list_sample # => []
Enter fullscreen mode Exit fullscreen mode

リストのコピー

list_sample =  ['a', 'b', 'c']
list_copy = list_sample.copy()
list_copy # => ['a', 'b', 'c']
Enter fullscreen mode Exit fullscreen mode

スライスによるリストの操作

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample[2:4] = ['1', '2', '3']
print(list_sample) # => ['a', 'b', '1', '2', '3', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample[3:] = []
print(list_sample) # => ['a', 'b', '1']
Enter fullscreen mode Exit fullscreen mode
list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample[2:7:2] = ['X', 'Y', 'Z']
list_sample # => ['a', 'b', 'X', 'd', 'Y', 'f', 'Z', 'h', 'i', 'j']
Enter fullscreen mode Exit fullscreen mode

ソート

list_sample =  ['a', 'c', 'A', 'b', 'C', 'B' ]
list_sample.sort()
list_sample # => ['A', 'B', 'C', 'a', 'b', 'c']
list_sample.sort(key=str.lower)
list_sample # => ['a', 'A', 'b', 'B', 'c', 'C']
list_sample.sort(key=str.lower, reverse=True)
list_sample # => ['c', 'C', 'b', 'B', 'a', 'A']
Enter fullscreen mode Exit fullscreen mode

順序逆転、逆順

list_sample =  ['a', 'b', 'c']
list_sample.reverse()
list_sample # => ['c', 'b', 'a']
Enter fullscreen mode Exit fullscreen mode

リストの繰り返し

list_sample = [1, 2]
list_sample * 3 #=> [1, 2, 1, 2, 1, 2]
list_sample *= 5 #=> [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
Enter fullscreen mode Exit fullscreen mode

リストの連結

+で末尾にリスト追加する。

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample + ['1','2', '3']
# => ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', '1', '2', '3']
Enter fullscreen mode Exit fullscreen mode
list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample.extend(['1','2', '3'])
list_sample # => ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', '1', '2', '3']
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide