「リストの中で一番大きな値を取得したい」「最小のスコアを調べたい」── Pythonでこれを簡単に実現するのが、max()
と min()
関数です。
この記事では、リストに対してmax()
/ min()
を使う基本的な方法から、いろいろな使い方までを、初心者にもわかりやすく説明します。
max()・min()の基本的な使い方

書き方
max(リスト) # 一番大きな値を取得
min(リスト) # 一番小さな値を取得
数字のリストで使う
# 数字のリストを作成
numbers = [10, 3, 25, 8]
# 一番大きな値と小さな値を取得
biggest = max(numbers)
smallest = min(numbers)
print(f"一番大きな値:{biggest}") # 25
print(f"一番小さな値:{smallest}") # 3
文字のリストで使う
数字だけでなく、文字のリストにも使えます(あいうえお順、アルファベット順)。
# 名前のリストを作成
names = ["田中", "佐藤", "鈴木"]
# あいうえお順で最後と最初
last_name = max(names) # 鈴木(すずき)
first_name = min(names) # 佐藤(さとう)
print(f"あいうえお順で最後:{last_name}")
print(f"あいうえお順で最初:{first_name}")
ポイント: 基本的なリストならmax()
・min()
でそのまま最大・最小が取得できます。
条件を指定して最大・最小を取得
文字の長さで比較する
# 動物の名前リスト
animals = ["犬", "ぞう", "ねこ"]
# 一番長い名前を取得
longest = max(animals, key=len)
shortest = min(animals, key=len)
print(f"一番長い名前:{longest}") # ぞう(2文字)
print(f"一番短い名前:{shortest}") # 犬(1文字)
辞書のリストで特定の値を比較
# 生徒の成績データ
students = [
{"名前": "田中", "点数": 80},
{"名前": "佐藤", "点数": 92},
{"名前": "鈴木", "点数": 75},
]
# 点数が一番高い生徒を取得
top_student = max(students, key=lambda x: x["点数"])
worst_student = min(students, key=lambda x: x["点数"])
print(f"最高点の生徒:{top_student['名前']}({top_student['点数']}点)")
print(f"最低点の生徒:{worst_student['名前']}({worst_student['点数']}点)")
3. 空のリストで使うときの注意点
エラーが起きる場合
# 空のリストを作成
empty_list = []
# エラーが発生!
# print(max(empty_list)) # ValueError: max() arg is an empty sequence
対策1:事前にリストが空かチェック
# 安全な使い方
if empty_list:
print(f"最大値:{max(empty_list)}")
else:
print("リストが空なので、最大値はありません")
対策2:デフォルト値を指定
# デフォルト値を指定(Python 3.4以降)
max_value = max(empty_list, default=0)
min_value = min(empty_list, default=0)
print(f"最大値:{max_value}") # 0
print(f"最小値:{min_value}") # 0
実際に使える例

例1:テストの点数分析
# テストの点数リスト
test_scores = [65, 80, 90, 70, 85]
# 最高点と最低点を取得
highest_score = max(test_scores)
lowest_score = min(test_scores)
# 平均点を計算
average_score = sum(test_scores) / len(test_scores)
print("=== テスト結果分析 ===")
print(f"最高点:{highest_score}点")
print(f"最低点:{lowest_score}点")
print(f"平均点:{average_score:.1f}点")
例2:一番人気の色を調べる
# 好きな色のアンケート結果
favorite_colors = ["赤", "青", "赤", "緑", "青", "赤", "黄"]
# 重複を取り除いてユニークな色を取得
unique_colors = set(favorite_colors)
# 一番多く選ばれた色を見つける
most_popular = max(unique_colors, key=favorite_colors.count)
least_popular = min(unique_colors, key=favorite_colors.count)
print(f"一番人気の色:{most_popular}({favorite_colors.count(most_popular)}票)")
print(f"一番不人気の色:{least_popular}({favorite_colors.count(least_popular)}票)")
例3:温度データの分析
# 1週間の気温データ
temperatures = [22.5, 25.1, 19.8, 23.7, 26.3, 21.2, 24.0]
days = ["月", "火", "水", "木", "金", "土", "日"]
# 最高気温と最低気温を取得
max_temp = max(temperatures)
min_temp = min(temperatures)
# その日を特定
max_day = days[temperatures.index(max_temp)]
min_day = days[temperatures.index(min_temp)]
print("=== 今週の天気まとめ ===")
print(f"最高気温:{max_temp}℃({max_day}曜日)")
print(f"最低気温:{min_temp}℃({min_day}曜日)")
例4:身長の比較
# クラスメートの身長データ
heights = [
{"名前": "田中", "身長": 165},
{"名前": "佐藤", "身長": 172},
{"名前": "鈴木", "身長": 158},
{"名前": "高橋", "身長": 180},
]
# 一番背が高い人と低い人を調べる
tallest = max(heights, key=lambda x: x["身長"])
shortest = min(heights, key=lambda x: x["身長"])
print(f"一番背が高い人:{tallest['名前']}さん({tallest['身長']}cm)")
print(f"一番背が低い人:{shortest['名前']}さん({shortest['身長']}cm)")
よくある応用パターン
パターン1:複数の条件で比較
# 商品の価格リスト
products = [
{"商品名": "りんご", "価格": 150, "産地": "青森"},
{"商品名": "みかん", "価格": 100, "産地": "愛媛"},
{"商品名": "ぶどう", "価格": 300, "産地": "山梨"},
]
# 最も高い商品と安い商品
most_expensive = max(products, key=lambda x: x["価格"])
cheapest = min(products, key=lambda x: x["価格"])
print(f"最も高い商品:{most_expensive['商品名']}({most_expensive['価格']}円)")
print(f"最も安い商品:{cheapest['商品名']}({cheapest['価格']}円)")
パターン2:絶対値で比較
# 基準点からの差を比較
target = 50
scores = [45, 55, 40, 60, 48]
# 50に最も近い値を見つける
closest = min(scores, key=lambda x: abs(x - target))
farthest = max(scores, key=lambda x: abs(x - target))
print(f"50に最も近い値:{closest}")
print(f"50から最も遠い値:{farthest}")
まとめ
Pythonのmax()
/ min()
関数は、リストの中から最大値・最小値を簡単に取り出す便利な機能です。
条件付きでも使えるようになれば、いろいろな分析に応用できます。
この記事のポイント
max(リスト)
/min(リスト)
で最大・最小を取得key=
を使えば、条件付きの最大・最小が可能- 空のリストに注意(
default=
で安全に対処) - 実用場面:点数集計、アンケート分析、データ比較など
コメント