Python Dictionaries - Key-Value Pairs and Data Structures
Master Python's Essential Key-Value Data Structure
Python Collections Overview
Lists
Ordered collections stored by index position in square brackets. Allow duplicates and are mutable.
Sets
Unordered collections in curly braces with no index values, no duplicates allowed.
Dictionaries
Collections that store items by keys instead of index positions. Use key-value pairs in curly braces.
This is lesson five in the Python programming series, building on variables, conditional logic, modules, and loops from previous lessons.
Dictionary vs List Structure
| Feature | Lists | Dictionaries |
|---|---|---|
| Storage Method | By index position | By key names |
| Brackets Used | Square brackets [] | Curly braces {} |
| Access Method | list[0] | dict['key'] |
| Ordering | Ordered sequence | No position concept |
Creating Your First Dictionary
Declare with Curly Braces
Start with dictionary_name = {} to create an empty dictionary structure.
Add Key-Value Pairs
Insert properties as 'key': value pairs, where keys are always in quotes and values match their data type.
Use Any Data Types
Values can be strings, numbers, booleans, lists, or even nested dictionaries as shown in the car example.
If you have JavaScript experience, Python dictionaries are equivalent to JavaScript objects, while Python lists correspond to JavaScript arrays.
Data Types in Car Dictionary Example
Unlike regular Python variables, dictionary keys can contain spaces because they are enclosed in quotes. However, use consistent naming conventions for better code readability.
Nested Dictionary Organization
Dictionary Access Methods
This is the primary way to access dictionary values in Python
Keys must be enclosed in quotes even if they look like variable names
Use dict['outer']['inner'] to access nested dictionary properties
Unlike lists, you cannot use dict[0] to get the first item
Dictionary properties cannot be accessed by index position. Using dict[0] will result in a KeyError because dictionaries don't have positional ordering.
Dictionary Modification Operations
Update Existing Values
Use dict['key'] = new_value to change the value of an existing property.
Add New Properties
Assign to a non-existent key to create it: dict['new_key'] = value.
Modify Nested Collections
Access nested lists or dictionaries first, then use their specific methods like append() or extend().
Toggle Boolean Values
Use the 'not' operator to flip boolean values: dict['bool_key'] = not dict['bool_key'].
Dictionary Deletion Methods
| Feature | del keyword | pop() method |
|---|---|---|
| Syntax | del dict['key'] | dict.pop('key') |
| Returns Value | No | Yes |
| Error if Run Twice | Yes | No (if default provided) |
| Use Case | Permanent removal | Remove and save value |
The pop() method allows you to remove a property while capturing its value, enabling you to rename properties or temporarily store data before reassignment.
This lesson is a preview from our Data Science & AI Certificate Online (includes software) and Python Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.
Key Takeaways