5
2

More than 3 years have passed since last update.

Pythonのlistの使い方まとめ

Posted at

はじめに

一度は勉強したけど、「あれ?これってどうだったっけ...」という方向けのまとめです。

FE(基本情報技術者試験)でもPythonが選択できるようになりました。
本来なら今年の春試験が初の出題となる予定でしたがコロナの影響で中止となりましたね。

そんなPythonのlistもFEの例題でバッチリ出題されているので、使い方をまとめてみました。

試験の直前に見返すも良し、普段のプログラミングで忘れがちなメソッドを思い出すも良し。
ぜひご活用ください。

listの初期化

まずは基本のlistの宣言から。

alist = []
blist = [1, 1, 2, 3, 5, 8, 13]
clist = list(range(5))
dlist = [i for i in range(10) if (i % 2 == 0)]  # リスト内包表記

print(f"alist = {alist}\nblist = {blist}\nclist = {clist}\ndlist = {dlist}")
output
alist = []
blist = [1, 1, 2, 3, 5, 8, 13]
clist = [0, 1, 2, 3, 4]
dlist = [0, 2, 4, 6, 8]

代入とコピー

意外と盲点です。なので、以降のlistの扱いでは参照渡しと値渡しの両方を交えて説明します。

  • = でlistを代入
    • 参照渡し
    • 代入元(alist)が変更 → blist も変わる
  • .copy() メソッドの返り値を代入
    • 値渡し
    • 代入元(alist)が変更 → clist は変化無し
alist = [1, 2, 3]

# 参照渡し
blist = alist

# 値渡し
clist = alist.copy()

def delete_middle(list_obj, msg):
    list_obj.pop(int(len(list_obj)/2))
    print(msg, list_obj)

delete_middle(alist, "alist =")
print(f"blist = {blist}")
print(f"clist = {clist}")
output
alist = [1, 3]
blist = [1, 3]
clist = [1, 2, 3]

listに要素を追加

  • 要素を後ろに1つだけ追加
    • .append(x) メソッド
  • 指定したindexの場所に1つ追加
    • .insert(index, obj) メソッド
  • listにlistを追加
    • + operator(演算子)を使う
    • 演算後に blistclist の要素が変化しても dlist には影響なし
alist = [1, 2, 3]
blist = alist
clist = alist.copy()

# 4を後ろに追加
alist.append(4)

# 1のindexに100を追加
alist.insert(1, 100)

# listにlistを追加
dlist = blist + clist

print(f"alist = {alist}\nblist = {blist}\nclist = {clist}\ndlist = {dlist}")
alist = [1, 100, 2, 3, 4]
blist = [1, 100, 2, 3, 4]
clist = [1, 2, 3]
dlist = [1, 100, 2, 3, 4, 1, 2, 3]

listの中身を削除

indexを指定して削除

alist = [1, 3, 5, 7, 9]
blist = alist
clist = alist.copy()

del alist[2]
print(f"del alist[2] =>\n\talist = {alist}\n\tblist = {blist}\n\tclist = {clist}\n")

alist.pop(2)
print(f"alist.pop(2) =>\n\talist = {alist}\n\tblist = {blist}\n\tclist = {clist}")
output
del alist[2] =>
    alist = [1, 3, 7, 9]
    blist = [1, 3, 7, 9]
    clist = [1, 3, 5, 7, 9]

alist.pop(2) =>
    alist = [1, 3, 9]
    blist = [1, 3, 9]
    clist = [1, 3, 5, 7, 9]

値を指定して削除

先頭側にある値だけが削除されます。その値全てではありません。

alist = [1, 2, 2, 3, 3, 3]
blist = alist
clist = alist.copy()

alist.remove(2)

print(f"alist = {alist}\nblist = {blist}\nclist = {clist}")
output
alist = [1, 2, 3, 3, 3]
blist = [1, 2, 3, 3, 3]
clist = [1, 2, 2, 3, 3, 3]

一番後ろの項目を削除

.pop() に引数を入れなければ .pop(-1) と同じで、リストの最後の要素が消されます。

alist = [1, 3, 5, 7, 9]
blist = alist
clist = alist.copy()

alist.pop()
print(f"alist.pop(2) =>\n\talist = {alist}\n\tblist = {blist}\n\tclist = {clist}")
output
alist.pop() =>
    alist = [1, 3, 5, 7]
    blist = [1, 3, 5, 7]
    clist = [1, 3, 5, 7, 9]

全て削除

alist = [1, 2, 3]
blist = alist
clist = alist.copy()

# alist = [] もやってることは同じ。
# alist = [] だと宣言と紛らわしいけど、開発チームのお好みで。
alist.clear()

print(f"alist = {alist}\nblist = {blist}\nclist = {clist}")
output
alist = []
blist = []
clist = [1, 2, 3]

値の順番で並び替える

alist = [20, 1, 5, 13, 8]
blist = alist
clist = alist.copy()

alist.sort()

print(f"alist = {alist}\nblist = {blist}\nclist = {clist}")
output
alist = [1, 5, 8, 13, 20]
blist = [1, 5, 8, 13, 20]
clist = [20, 1, 5, 13, 8]

最初に現れる値のindexを調べる

alist = [333, 1, 333, 22, 333, 22]
# 22が何番目にあるか
index = alist.index(22)
print(f"index = {index}")
output
index = 3

値が何個含まれているか

alist = [0, 1, 1, 0, 1, 0 ,1, 0, 0]
# 1が何回出てきたか
count = alist.count(1)
print(f"count = {count}")
output
count = 4

さいごに

メジャーどころのlistのメソッドは抑えたかなと思います。
参照渡しと値渡しの挙動を説明したくて返って読みにくくなってしまいました。すみませんmm

忘備録としてお使いいただければと幸いです。それでは。

5
2
0

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
5
2