「Pythonのタプルって何?」
「リストとどこが違うの?」
「変更できないデータなんて使いみちあるの?」
このような疑問を持ったことはありませんか?タプルは、Pythonでデータをまとめて扱うためのデータ型の一つです。
リストに似ていますが、「一度作ったら中身を変更できない」という大きな特徴があります。
この記事では、タプルの基本的な使い方から、リストとの違い、実用的な活用方法まで、初心者の方にもわかりやすく解説します。
タプルの基本的な作り方と使い方

タプルを作る
タプルは、丸カッコ()
とカンマ,
を使って作ります。
# 基本的なタプルの作成
colors = ("赤", "青", "緑")
numbers = (1, 2, 3, 4, 5)
mixed = ("apple", 10, 3.14, True)
print(f"色のタプル: {colors}")
print(f"数字のタプル: {numbers}")
print(f"混合タプル: {mixed}")
実行結果:
色のタプル: ('赤', '青', '緑')
数字のタプル: (1, 2, 3, 4, 5)
混合タプル: ('apple', 10, 3.14, True)
タプルの要素にアクセスする
リストと同じように、インデックス番号で要素にアクセスできます。
fruits = ("りんご", "バナナ", "オレンジ")
print(f"最初の果物: {fruits[0]}") # りんご
print(f"2番目の果物: {fruits[1]}") # バナナ
print(f"最後の果物: {fruits[-1]}") # オレンジ
実行結果:
最初の果物: りんご
2番目の果物: バナナ
最後の果物: オレンジ
タプルの長さを調べる
coordinates = (10, 20, 30)
print(f"座標の数: {len(coordinates)}")
実行結果:
座標の数: 3
タプルとリストの重要な違い
表記方法の違い
# タプル:丸カッコを使用
tuple_data = ("a", "b", "c")
print(f"タプル: {tuple_data}")
print(f"型: {type(tuple_data)}")
# リスト:角カッコを使用
list_data = ["a", "b", "c"]
print(f"リスト: {list_data}")
print(f"型: {type(list_data)}")
実行結果:
タプル: ('a', 'b', 'c')
型: <class 'tuple'>
リスト: ['a', 'b', 'c']
型: <class 'list'>
変更可能性の違い
これが最も重要な違いです。
# リストは変更可能
my_list = [1, 2, 3]
my_list[0] = 10 # 要素を変更
my_list.append(4) # 要素を追加
print(f"変更後のリスト: {my_list}")
# タプルは変更不可能
my_tuple = (1, 2, 3)
# my_tuple[0] = 10 # エラー!変更できません
# my_tuple.append(4) # エラー!追加もできません
print(f"タプル: {my_tuple}")
実行結果:
変更後のリスト: [10, 2, 3, 4]
タプル: (1, 2, 3)
比較表
特徴 | タプル | リスト |
---|---|---|
表記 | () | [] |
変更 | ❌ 不可 | ✅ 可能 |
要素追加 | ❌ 不可 | ✅ 可能 |
要素削除 | ❌ 不可 | ✅ 可能 |
処理速度 | 少し速い | 標準 |
用途 | 固定データ | 可変データ |
1つの要素だけのタプルの注意点

間違いやすいパターン
# これはタプルではなく、ただの数値
not_tuple = (10)
print(f"型: {type(not_tuple)}") # <class 'int'>
# 正しいタプルの作り方(カンマが必要)
real_tuple = (10,)
print(f"型: {type(real_tuple)}") # <class 'tuple'>
実行結果:
型: <class 'int'>
型: <class 'tuple'>
カンマが重要な理由
# カンマがないと、ただの括弧として解釈される
value1 = (5) # これは数値の5
value2 = (5,) # これは要素が1つのタプル
print(f"value1: {value1}, 型: {type(value1)}")
print(f"value2: {value2}, 型: {type(value2)}")
# 実用例:座標の1次元版
point_1d = (42,) # 1次元の座標
print(f"1次元座標: {point_1d}")
実行結果:
value1: 5, 型: <class 'int'>
value2: (5,), 型: <class 'tuple'>
1次元座標: (42,)
タプルの便利な使い方
複数の値を一度に代入(アンパック)
タプルの最も便利な機能の一つです。
# 座標データのアンパック
coordinate = (10, 20)
x, y = coordinate
print(f"x座標: {x}, y座標: {y}")
# 名前と年齢のアンパック
person = ("田中", 25)
name, age = person
print(f"名前: {name}, 年齢: {age}")
# 3つの値のアンパック
rgb = (255, 128, 0)
red, green, blue = rgb
print(f"赤: {red}, 緑: {green}, 青: {blue}")
実行結果:
x座標: 10, y座標: 20
名前: 田中, 年齢: 25
赤: 255, 緑: 128, 青: 0
変数の値を交換する
# 従来の方法(一時変数が必要)
a = 5
b = 10
temp = a
a = b
b = temp
print(f"従来の方法: a={a}, b={b}")
# タプルを使った方法(スマート!)
x = 5
y = 10
x, y = y, x
print(f"タプル活用: x={x}, y={y}")
実行結果:
従来の方法: a=10, b=5
タプル活用: x=10, y=5
関数から複数の値を返す
def calculate_stats(numbers):
"""数値リストの統計を計算して複数の値を返す"""
total = sum(numbers)
count = len(numbers)
average = total / count if count > 0 else 0
return total, count, average # タプルで返す
# 関数を使用
scores = [85, 92, 78, 96, 88]
sum_score, num_tests, avg_score = calculate_stats(scores)
print(f"合計点: {sum_score}")
print(f"テスト数: {num_tests}")
print(f"平均点: {avg_score:.1f}")
実行結果:
合計点: 439
テスト数: 5
平均点: 87.8
for文での同時展開
# 名前と年齢のペアのリスト
students = [("田中", 20), ("佐藤", 22), ("鈴木", 19)]
print("学生名簿:")
for name, age in students:
print(f" {name}さん({age}歳)")
# 座標のリスト
points = [(0, 0), (1, 2), (3, 4), (5, 6)]
print("\n座標リスト:")
for x, y in points:
print(f" 点({x}, {y})")
実行結果:
学生名簿:
田中さん(20歳)
佐藤さん(22歳)
鈴木さん(19歳)
座標リスト:
点(0, 0)
点(1, 2)
点(3, 4)
点(5, 6)
タプルができること・できないこと
できること
# インデックスでのアクセス
data = ("apple", "banana", "cherry")
print(f"最初の要素: {data[0]}")
# スライス
print(f"最初の2つ: {data[:2]}")
# 要素数の取得
print(f"要素数: {len(data)}")
# 要素の検索
if "banana" in data:
print("バナナが含まれています")
# 要素の数を数える
numbers = (1, 2, 2, 3, 2)
print(f"2の個数: {numbers.count(2)}")
# 要素の位置を調べる
print(f"最初の2の位置: {numbers.index(2)}")
実行結果:
最初の要素: apple
最初の2つ: ('apple', 'banana')
要素数: 3
バナナが含まれています
2の個数: 3
最初の2の位置: 1
できないこと
colors = ("赤", "青", "緑")
# これらはすべてエラーになります
# colors[0] = "黄色" # 要素の変更
# colors.append("紫") # 要素の追加
# colors.remove("青") # 要素の削除
# del colors[1] # 要素の削除
print("タプルは変更できません")
入れ子のタプル(ネストしたタプル)
基本的な入れ子構造
# 座標の集合
coordinates = ((0, 0), (1, 2), (3, 4))
print(f"座標集合: {coordinates}")
# 最初の座標を取得
first_point = coordinates[0]
print(f"最初の座標: {first_point}")
# 最初の座標のx値を取得
first_x = coordinates[0][0]
print(f"最初の座標のx値: {first_x}")
実行結果:
座標集合: ((0, 0), (1, 2), (3, 4))
最初の座標: (0, 0)
最初の座標のx値: 0
複雑な構造の例
# 学生の情報(名前、年齢、成績のタプル)
students_data = (
("田中", 20, (85, 92, 78)),
("佐藤", 22, (90, 88, 95)),
("鈴木", 19, (82, 85, 89))
)
print("学生の詳細情報:")
for name, age, grades in students_data:
math, english, science = grades
average = sum(grades) / len(grades)
print(f" {name}さん({age}歳): 数学{math}, 英語{english}, 理科{science}, 平均{average:.1f}")
実行結果:
学生の詳細情報:
田中さん(20歳): 数学85, 英語92, 理科78, 平均85.0
佐藤さん(22歳): 数学90, 英語88, 理科95, 平均91.0
鈴木さん(19歳): 数学82, 英語85, 理科89, 平均85.3
辞書のキーとしてタプルを使う

座標をキーとした辞書
# 地図上の位置と地名の対応
locations = {
(35.6762, 139.6503): "東京",
(34.6937, 135.5023): "大阪",
(43.0642, 141.3469): "札幌"
}
# 座標で地名を検索
tokyo_coords = (35.6762, 139.6503)
if tokyo_coords in locations:
print(f"座標{tokyo_coords}は{locations[tokyo_coords]}です")
# 全ての場所を表示
print("\n登録されている場所:")
for (lat, lng), city in locations.items():
print(f" {city}: 緯度{lat}, 経度{lng}")
実行結果:
座標(35.6762, 139.6503)は東京です
登録されている場所:
東京: 緯度35.6762, 経度139.6503
大阪: 緯度34.6937, 経度135.5023
札幌: 緯度43.0642, 経度141.3469
ゲームの座標管理
# チェス盤のような座標システム
chess_board = {
(1, 1): "ルーク",
(1, 2): "ナイト",
(1, 3): "ビショップ",
(2, 1): "ポーン",
(2, 2): "ポーン"
}
def get_piece(row, col):
"""指定した位置の駒を取得"""
position = (row, col)
return chess_board.get(position, "空")
# 駒の位置を確認
print(f"(1,1)の駒: {get_piece(1, 1)}")
print(f"(2,1)の駒: {get_piece(2, 1)}")
print(f"(3,3)の駒: {get_piece(3, 3)}")
実行結果:
(1,1)の駒: ルーク
(2,1)の駒: ポーン
(3,3)の駒: 空
タプルの実用的な活用例
設定値の管理
# アプリケーションの設定(変更されたくない値)
APP_CONFIG = (
"MyApp", # アプリ名
"1.2.3", # バージョン
"production", # 環境
80, # ポート番号
True # デバッグモード
)
app_name, version, environment, port, debug = APP_CONFIG
print(f"アプリケーション: {app_name}")
print(f"バージョン: {version}")
print(f"環境: {environment}")
print(f"ポート: {port}")
print(f"デバッグモード: {debug}")
実行結果:
アプリケーション: MyApp
バージョン: 1.2.3
環境: production
ポート: 80
デバッグモード: True
データベースのレコード表現
# データベースの行をタプルで表現
users = [
(1, "田中太郎", "tanaka@example.com", 25),
(2, "佐藤花子", "sato@example.com", 30),
(3, "鈴木一郎", "suzuki@example.com", 28)
]
print("ユーザー一覧:")
for user_id, name, email, age in users:
print(f" ID:{user_id} {name} ({email}) {age}歳")
# 特定の条件でフィルタリング
adults = [user for user in users if user[3] >= 30]
print("\n30歳以上のユーザー:")
for user_id, name, email, age in adults:
print(f" {name} ({age}歳)")
実行結果:
ユーザー一覧:
ID:1 田中太郎 (tanaka@example.com) 25歳
ID:2 佐藤花子 (sato@example.com) 30歳
ID:3 鈴木一郎 (suzuki@example.com) 28歳
30歳以上のユーザー:
佐藤花子 (30歳)
関数の引数として複数の値を渡す
def draw_rectangle(position, size, color):
"""四角形を描画する関数"""
x, y = position
width, height = size
r, g, b = color
print(f"四角形を描画:")
print(f" 位置: ({x}, {y})")
print(f" サイズ: {width} x {height}")
print(f" 色: RGB({r}, {g}, {b})")
# タプルを使って引数を渡す
pos = (10, 20)
size = (100, 50)
color = (255, 0, 128)
draw_rectangle(pos, size, color)
実行結果:
四角形を描画:
位置: (10, 20)
サイズ: 100 x 50
色: RGB(255, 0, 128)
パフォーマンスの比較

メモリ使用量と速度
import sys
import time
# 同じデータでタプルとリストを作成
data_tuple = (1, 2, 3, 4, 5)
data_list = [1, 2, 3, 4, 5]
# メモリ使用量の比較
print(f"タプルのメモリ使用量: {sys.getsizeof(data_tuple)} bytes")
print(f"リストのメモリ使用量: {sys.getsizeof(data_list)} bytes")
# アクセス速度の簡単な比較
big_tuple = tuple(range(1000000))
big_list = list(range(1000000))
# タプルのアクセス時間
start = time.time()
for _ in range(1000):
_ = big_tuple[500000]
tuple_time = time.time() - start
# リストのアクセス時間
start = time.time()
for _ in range(1000):
_ = big_list[500000]
list_time = time.time() - start
print(f"\nアクセス時間(1000回):")
print(f"タプル: {tuple_time:.6f}秒")
print(f"リスト: {list_time:.6f}秒")
一般的に、タプルの方がメモリ効率が良く、アクセスも少し高速です。
よくある質問と解決方法
Q: タプルをリストに変換したい
my_tuple = (1, 2, 3, 4, 5)
my_list = list(my_tuple)
print(f"タプル: {my_tuple}")
print(f"リスト: {my_list}")
# リストで操作してからタプルに戻す
my_list.append(6)
new_tuple = tuple(my_list)
print(f"新しいタプル: {new_tuple}")
Q: タプルに要素を「追加」したい
タプルは変更できませんが、新しいタプルを作ることはできます。
original = (1, 2, 3)
# 要素を「追加」(実際は新しいタプル作成)
new_tuple = original + (4, 5)
print(f"元のタプル: {original}")
print(f"新しいタプル: {new_tuple}")
# 複数のタプルを結合
tuple1 = (1, 2)
tuple2 = (3, 4)
tuple3 = (5, 6)
combined = tuple1 + tuple2 + tuple3
print(f"結合したタプル: {combined}")
Q: タプルの一部だけを変更したい
def replace_tuple_element(original_tuple, index, new_value):
"""タプルの指定位置の要素を置き換えた新しいタプルを作成"""
temp_list = list(original_tuple)
temp_list[index] = new_value
return tuple(temp_list)
# 使用例
colors = ("赤", "青", "緑")
new_colors = replace_tuple_element(colors, 1, "黄色")
print(f"元のタプル: {colors}")
print(f"変更後のタプル: {new_colors}")
まとめ
タプルは、Pythonで「変更されない複数の値の組み合わせ」を表現するのに最適なデータ型です。
重要なポイント
- タプルは作成後に変更できない(イミュータブル)
- 丸カッコ
()
とカンマ,
で作成 - 1要素のタプルにはカンマが必須:
(value,)
- リストより少しメモリ効率が良い
タプルが適している場面
- 設定値や定数の管理
- 関数からの複数の戻り値
- 辞書のキーとして使用
- 座標や色の値など、セットで扱うデータ
- データベースのレコード表現
タプルが適していない場面
- 頻繁に要素を追加・削除する必要がある
- データの並び順を変更したい
- 要素の値を更新する必要がある
よく使うパターン
用途 | 例 |
---|---|
座標 | (x, y) |
RGB色 | (255, 128, 0) |
複数戻り値 | return name, age, score |
アンパック | x, y = point |
設定値 | CONFIG = ("app", "1.0", True) |
タプルを適切に使いこなすことで、より安全で読みやすいPythonコードを書けるようになります。
コメント