Skip to main content
April 2, 2026Brian McClain/6 min read

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.

Course Progress Checkpoint

This is lesson five in the Python programming series, building on variables, conditional logic, modules, and loops from previous lessons.

Dictionary vs List Structure

FeatureListsDictionaries
Storage MethodBy index positionBy key names
Brackets UsedSquare brackets []Curly braces {}
Access Methodlist[0]dict['key']
OrderingOrdered sequenceNo position concept
Recommended: Use dictionaries when you need named access to values rather than positional access.

Creating Your First Dictionary

1

Declare with Curly Braces

Start with dictionary_name = {} to create an empty dictionary structure.

2

Add Key-Value Pairs

Insert properties as 'key': value pairs, where keys are always in quotes and values match their data type.

3

Use Any Data Types

Values can be strings, numbers, booleans, lists, or even nested dictionaries as shown in the car example.

JavaScript Developers Note

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

Strings25%
Numbers25%
Lists13%
Booleans25%
Nested Dict13%
Key Naming Flexibility

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

Pros
Groups related properties logically together
Reduces clutter in main dictionary structure
Makes code more maintainable and readable
Reflects real-world data relationships better
Cons
Requires more complex access syntax with multiple brackets
Can make simple lookups more verbose
Increases nesting complexity for beginners

Dictionary Access Methods

0/4
Common Access Error

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

1

Update Existing Values

Use dict['key'] = new_value to change the value of an existing property.

2

Add New Properties

Assign to a non-existent key to create it: dict['new_key'] = value.

3

Modify Nested Collections

Access nested lists or dictionaries first, then use their specific methods like append() or extend().

4

Toggle Boolean Values

Use the 'not' operator to flip boolean values: dict['bool_key'] = not dict['bool_key'].

Dictionary Deletion Methods

Featuredel keywordpop() method
Syntaxdel dict['key']dict.pop('key')
Returns ValueNoYes
Error if Run TwiceYesNo (if default provided)
Use CasePermanent removalRemove and save value
Recommended: Use pop() when you need to save the deleted value, use del for permanent removal.
Safe Property Management

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.

Welcome back to our comprehensive course on Python programming and data science. I'm Brian McLean, and in this fifth lesson, we're diving into one of Python's most powerful and versatile data structures: dictionaries.

As you've noticed, each lesson in our series tackles a fundamental programming concept that builds your expertise systematically. We started with variables and data types in lesson one, explored conditional logic with if-else statements in lesson two, covered modules in lesson three, and just completed our deep dive into loops and string methods in lesson four.

Today, we're working in file five, and I'll begin by importing pprint—Python's pretty-print module. Since dictionaries can become complex, nested structures, pretty-printing will make our output far more readable and professionally formatted when we're debugging or analyzing data.

Understanding dictionaries is crucial because they're everywhere in modern Python development. From JSON APIs to configuration files, from database records to machine learning datasets, dictionaries form the backbone of data manipulation in Python. Think of them as Python's equivalent to real-world lookup tables—incredibly efficient and intuitive once you master their syntax.

A dictionary belongs to Python's family of collection data types—iterable structures that can hold multiple values. You're already familiar with lists, which store items by numerical index in square brackets, and sets, which store unique items in curly braces without any ordering or indexing.

Dictionaries occupy a unique middle ground: they store items in curly braces like sets, but unlike both lists and sets, they organize data using keys instead of positions. This key-value pair system makes dictionaries incredibly powerful for representing real-world relationships where you need to associate meaningful names with data.

Let's create our first dictionary to see this concept in action. We'll build a car dictionary that demonstrates how dictionaries excel at modeling complex, related data—something you'll encounter constantly in data science and web development.

Dictionary syntax follows a specific pattern: all key-value pairs go inside curly braces, with keys always enclosed in quotes (since they're technically strings) and values taking whatever data type is appropriate. Here's what makes dictionaries particularly elegant—values can be any Python data type, including other dictionaries, creating powerful nested structures.

Let's construct our car dictionary with multiple properties. If you're coming from JavaScript, you'll find this familiar—Python dictionaries are essentially JavaScript objects, while Python lists correspond to JavaScript arrays.

```python car = { "make": "Ford", "model": "Mustang GT", "options": ["leather seats", "premium sound", "sports package"], "year": 2003, "miles": 56789, "for sale": True, "on road": False } ```

Notice how we're mixing data types strategically. Our keys—make, model, options, year, miles, for sale, and on road—each serve as descriptive labels. The values span strings, numbers, lists, and booleans, demonstrating dictionary flexibility.

Pay particular attention to keys like "for sale" and "on road"—unlike regular Python variables, dictionary keys can contain spaces because they're quoted strings. This makes dictionaries more readable and closer to natural language, a significant advantage when modeling real-world data.

Let's enhance our dictionary with nested structures, which are essential for organizing related data professionally. Instead of having separate "MPG city" and "MPG highway" keys, we'll create a cleaner, more maintainable structure:

```python car = { "make": "Ford", "model": "Mustang GT", "options": ["leather seats", "premium sound", "sports package"], "year": 2003, "miles": 56789, "for sale": True, "on road": False, "MPG": { "city": 18, "highway": 24 } } ```


This nested approach demonstrates professional data modeling—grouping related properties under a parent key creates more logical, maintainable code structures that scale well in production environments.

Now let's master dictionary access patterns, which differ significantly from list indexing. To retrieve values, use the syntax `dictionary[key]`—note the square brackets with quoted keys, not dot notation like you might use in JavaScript.

```python print(car["make"]) # Ford print(car["model"]) # Mustang GT print(car["year"]) # 2003 print(car["for sale"]) # True ```

For nested data, you'll chain your lookups logically. To access the highway MPG, you first access the "MPG" key (which returns a dictionary), then access the "highway" key of that nested dictionary:

```python print(car["MPG"]["highway"]) # 24 ```

When working with list values within dictionaries, combine dictionary and list access patterns. To get the first option or a specific option by index:

```python print(car["options"]) # ['leather seats', 'premium sound', 'sports package'] print(car["options"][0]) # leather seats print(car["options"][2]) # sports package ```

Here's a critical concept: dictionaries don't support numerical indexing like lists. Attempting `car[0]` will raise a KeyError because Python looks for a key named "0", not a positional element. This is by design—dictionaries prioritize meaningful key names over positional access.

Updating dictionary values uses the same syntax as reading them, but with assignment. This makes dictionary manipulation intuitive and consistent:

```python car["year"] = 2002 car["miles"] = 57890 pprint(car) # Shows updated values ```

For list values within dictionaries, use standard list methods. To add a new option:

```python car["options"].append("sunroof") ```

Boolean manipulation in dictionaries follows standard Python patterns. To flip a boolean value regardless of its current state:


```python car["for sale"] = not car["for sale"] ```

This technique is particularly useful in data processing workflows where you need to toggle states based on conditions.

Dictionary management includes both destructive and non-destructive deletion methods. The `del` keyword permanently removes key-value pairs:

```python del car["on road"] # Permanently removes this property ```

For safer deletion that preserves data, use the `pop()` method, which removes and returns the value:

```python saved_miles = car.pop("miles") # Remove but save the value # Later, if needed: car["mileage"] = saved_miles # Restore under a new key name ```

Adding new properties to dictionaries is remarkably straightforward—simply assign a value to a new key. If the key exists, you update it; if it doesn't exist, Python creates it automatically:

```python car["doors"] = 2 # Creates new property car["doors"] = 4 # Updates existing property ```

Let's put these concepts together with a practical challenge that reinforces both dictionary manipulation and list methods. We'll add three new options—subwoofer, CD player, and flame decals—then alphabetize the complete options list:

```python # Add multiple options efficiently using extend() car["options"].extend(["subwoofer", "CD player", "flame decals"]) # Alphabetize the options list in place car["options"].sort() pprint(car) # Display the updated, organized dictionary ```

This exercise demonstrates real-world data management patterns—extending lists within dictionaries and maintaining sorted data for better organization and searchability. These techniques become essential when working with larger datasets in data science applications or managing configuration data in production systems.

  1. Understanding dictionary fundamentals prepares you for advanced Python concepts like JSON processing, API interactions, and database operations—all critical skills in today's data-driven development landscape.

Key Takeaways

1Dictionaries are collections that store data as key-value pairs in curly braces, accessed by key names rather than index positions
2Keys must always be enclosed in quotes and can contain spaces, unlike regular Python variable names
3Dictionary values can be any data type including strings, numbers, booleans, lists, and nested dictionaries
4Access dictionary properties using square bracket notation: dict['key'], with chaining for nested structures: dict['outer']['inner']
5Dictionaries have no index-based access - attempting dict[0] will result in a KeyError since there's no positional ordering
6Modify dictionaries by direct assignment: dict['key'] = new_value creates new properties or updates existing ones
7Use del dict['key'] for permanent removal or dict.pop('key') to remove while saving the deleted value
8The pprint module improves dictionary readability when displaying complex nested structures in the console

RELATED ARTICLES