0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

リストの基本操作【Day 13】

0
Last updated at Posted at 2025-12-12

Qiita Advent Calendar 2025 のパイソニスタの一人アドカレ Day13 の記事です。

リストの基本操作

リストは Python で最もよく使われるデータ構造のひとつです。
複数の値をまとめて扱えるため、これを理解しておくとコードが一気に書きやすくなります。

リストの作り方

fruits = ["apple", "banana", "orange"]
numbers = [1, 2, 3, 4]

中身は なんでも混ぜてOK ですが、用途ごとに揃えておくのが基本です。

要素の取り出し(インデックス)

番号(0から始まる)でアクセスします。

fruits = ["apple", "banana", "orange"]

print(fruits[0])  # apple
print(fruits[2])  # orange

要素の変更

fruits[1] = "grape"
print(fruits)  # ["apple", "grape", "orange"]

要素の追加

append(最後に追加)

fruits.append("melon")

insert(好きな位置に追加)

fruits.insert(1, "kiwi")  # 1番目に挿入

要素の削除

remove(値を指定して削除)

fruits.remove("banana")

pop(インデックスで削除)

fruits.pop(0)  # 先頭を削除

リストの長さを調べる

len(fruits)

リストを使ったよくあるパターン

合計値

numbers = [1, 2, 3]
print(sum(numbers))  # 6

最大・最小

max(numbers)  # 3
min(numbers)  # 1

含まれているかチェック

if "apple" in fruits:
    print("入っています")

まとめ

  • リストは「複数の値をまとめる」基本のデータ型
  • インデックスは 0 から始まる
  • append / insert / remove / pop は最重要
  • sum / max / min / in など組み込み関数も便利
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?