Python Dictionaries

Python · Dictionaries

Theory

A dictionary (dict) stores key-value pairs: employee = {'name': 'Asha', 'salary': 65000}. You look a value up by its key, employee['salary'] gives 65000, instead of by position. Assigning to a key either updates it, if it exists, or adds a new pair: employee['dept'] = 'IT'. Reading a key that does not exist with square brackets raises KeyError, which is why .get() exists. Keys are unique; assigning the same key again simply overwrites the old value.

A dict is a hash table. Python hashes the key to find the slot where its value lives, so lookups, inserts and deletes take roughly constant time on average (O(1)) even with millions of entries. That is why keys must be hashable (str, int, tuple), while values can be anything, including lists and other dicts. Since Python 3.7, dicts keep their insertion order, so looping returns keys in the order you added them. key in employee checks keys, not values.

Why we use it

Most real-world records are naturally 'field: value': a customer, a config, a JSON response. A dict lets you reach any field instantly by name, and it is the go-to structure for lookups, counting and grouping.

When to use it

Use a dict whenever you look things up by a label: ID to record, word to count, code to description. Use a list when position is what matters. For counting use collections.Counter; for default values on missing keys use collections.defaultdict.

How it works

  1. Evaluate employee['salary']
  2. Compute hash('salary') to find the slot
  3. Is the key 'salary' stored there? Yes: Return its value, 65000. No: Raise KeyError: 'salary'.
  4. Assignment employee['dept'] = 'IT' hashes 'dept' the same way
  5. Does 'dept' already exist? Yes: Overwrite its value. No: Add a new pair at the end.
  6. print(employee['salary']) shows 65000

Syntax

my_dict = {"key": "value"}

Example

employee = {'name': 'Asha', 'salary': 65000}
employee['salary']

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic