Skip to Content
Effective Python 第3版 ―Pythonプログラムを改良する125項目
book

Effective Python 第3版 ―Pythonプログラムを改良する125項目

by Brett Slatkin, 鈴木 駿
October 2025
Intermediate to advanced
572 pages
7h 46m
Japanese
O'Reilly Japan, Inc.
Content preview from Effective Python 第3版 ―Pythonプログラムを改良する125項目

4章辞書

リストやシーケンスを自然に補完するデータ構造として辞書があります。辞書とはキーとそれに対応する値を格納するデータ構造です。他のプログラミング言語では連想配列またはハッシュテーブルとも呼ばれています。その多用途性から、動的に追加されたり変更されたりするデータを管理するデータ構造として最適です。新規にプログラムを書く際、データ構造やクラスが確定する前のデータ構造としては辞書を使うとよいでしょう。

辞書における要素の追加と削除は(償却計算量において)定数時間で実行され、単純なリストよりも優れています。そのため、辞書がPythonにおけるオブジェクト指向機能を実現するためのデータ構造であることも理解できるでしょう。また、他のプログラミング言語における連想配列やハッシュテーブルにはない機能を提供する標準ライブラリも用意されています。

項目25 辞書の挿入順序に依存する際は注意する

Python 3.5以前は、辞書をイテレートするとキーが任意の順序で返されました。つまり、そのイテレートの順序と辞書に挿入した順序は一致しませんでした。動物とその子供を対応付ける辞書の例で説明します。

# Python 3.5
baby_names = {
    "cat": "kitten",
    "dog": "puppy",
}
print(baby_names)  # {"dog": "puppy", "cat": "kitten"}

辞書の挿入順は"cat""dog"ですが、出力された順序は"dog""cat"でした。この動きは驚くべきものであり、テストケースの再現が難しくなり、デバッグの難易度が上昇します。特に、Python初心者には混乱の元になる動きでした。

このような動きだった理由は、Python ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Think Python 第3版 ―コンピュータ科学者の思考で問題を解決する

Think Python 第3版 ―コンピュータ科学者の思考で問題を解決する

Allen B. Downey, 大橋 真也
入門 Python 3 第2版

入門 Python 3 第2版

Bill Lubanovic, 鈴木 駿, 長尾 高弘

Publisher Resources

ISBN: 9784814401338Publisher Website