dictionary
A Python collection that stores values under keys, written in curly brackets, such as {"tea": 250}.
In more detail
Dictionaries are hash tables, so looking up, adding and deleting by key take constant time on average. Keys must be hashable, and since Python 3.7 a dict remembers the order in which keys were inserted. Reading a missing key with d[key] raises KeyError, while d.get(key) returns None.
Examples
users = {'alice': 1, 'bob': 2}; 'bob' in users # a hash lookup, not a scan{'amount': 100}The fold lessons use a dictionary as the simplest state store.spanish = {"hello": "hola", "red": "rojo"}
print(spanish["red"])In this course: Looking up a missing key raises KeyError. When pytest compares two dictionaries that differ, it names the differing keys and hides the identical ones.Related terms
- hash table — A data structure that stores key-value pairs in an array and uses a hash of each key to jump almost directly to its slot.
- set — Python's built-in unordered collection of unique items, implemented as a hash table that stores keys without values.
- hashable — Describes a Python object that has a hash value that never changes during its lifetime and can be compared for equality, so it can be a dict key or set member.
- key-value pair — An entry that links a key, used to look something up, to the value stored for it, such as a user name mapped to a user record.
- KeyError — The Python exception raised when you look up a dictionary key that does not exist.
- state store — Local storage where a stream processing application keeps the current results of its computations.
- json module — The Python standard library module that converts between Python values and JSON text.
- dictionary unpacking — Writing ** before a dictionary in a function call to pass each of its key-value pairs as a keyword argument.
- tuple — An ordered, fixed-length group of values in which each position can have its own meaning and type.
- assertion rewriting — pytest's trick of rewriting plain assert statements as it imports test files so failures print the values involved.
Taught in
- Streaming Responses — AI Engineering: LLM Apps, RAG & Agents
- Structured Output — AI Engineering: LLM Apps, RAG & Agents
- Agent SDKs And Human Approval — AI Engineering: LLM Apps, RAG & Agents
- Why Complexity Matters In Production — Algorithms & Data Structures for Working Engineers
- Hash Tables: Chaining, Open Addressing And Load Factor — Algorithms & Data Structures for Working Engineers
- Python Dicts And Sets Under The Hood — Algorithms & Data Structures for Working Engineers
- Collisions In Practice And Hash Flooding — Algorithms & Data Structures for Working Engineers
- Choosing The Right Sequence — Algorithms & Data Structures for Working Engineers
- Representing A Graph: Matrix Or Adjacency List — Algorithms & Data Structures for Working Engineers
- Ninety Minutes, No Question Count — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- The Field The Registry Refused — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- Forty Minutes To Count Again — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- The Alert That Came An Hour Late — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- Twelve Lines Instead Of Four Hundred — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- Duplicates Are The Default — Apache Kafka & Event Streaming
- Headers, Envelopes And Event Metadata — Apache Kafka & Event Streaming
- Log Compaction: A Topic As A Table — Apache Kafka & Event Streaming
- Materialised Views From A Stream — Apache Kafka & Event Streaming
- A Sample Program, Explained — Hands-on Python: Complete Video Course & Book
- The String Format Method — Hands-on Python: Complete Video Course & Book
- Local Scope And Global Constants — Hands-on Python: Complete Video Course & Book
- Dictionaries — Hands-on Python: Complete Video Course & Book
- Dictionaries And String Formatting — Hands-on Python: Complete Video Course & Book
- Lists, Tuples, Sets, Dicts: Choosing One — Hands-on Python: Complete Video Course & Book
- One Assertion Per Reason — Automated Testing, TDD & Quality Engineering
- Fixtures And Their Scope — Automated Testing, TDD & Quality Engineering
- Making Failures Explain Themselves — Automated Testing, TDD & Quality Engineering
- The Exception Family Tree — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Thirty Questions, One Sneaky Print — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Tuples, And The Comma That Makes Them — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Dictionaries: Keys, Values And Items — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Strings: Escapes, Slices And Methods — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Collections Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Twelve Traps, One Shape Each — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Your Own Modules And Packages — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Modules Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- String Methods The Exam Picks — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Class Variables, Instance Variables And __dict__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Introspection: hasattr, __name__, __module__ And __bases__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Special Methods: __str__, __eq__ And Friends — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Object-Oriented Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Lambdas, map() And filter() — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
References
Certification courses
- AWS AIP-C01 course and practice exam: AI engineering with LLM apps, RAG and agents
- CCDAK and CCAAK course and practice exams: Confluent Apache Kafka certifications
- PCEP course and practice exam: Certified Entry-Level Python Programmer
- PCAP course and practice exam: Certified Associate in Python Programming