Skip to main content
March 23, 2026Brian McClain/2 min read

Python Programming Challenge #6 - Making Dictionaries of Filtered Items

Master Python Dictionary Filtering and Data Manipulation

Challenge Overview

This programming challenge focuses on filtering dictionaries based on multiple criteria while dynamically adding new key-value pairs during processing.

Core Concepts Covered

Dictionary Access

Learn to access nested dictionary values using square bracket notation. Essential for working with structured data in Python.

Filtering Operations

Apply multiple conditional filters to select items based on price range and stock availability criteria.

Dynamic Key Addition

Add calculated fields on-the-fly by creating new dictionary keys with computed values during iteration.

Video Transcription

Working with lists of dictionaries is a fundamental skill in Python data manipulation, particularly when processing structured datasets. In this example, each dictionary contains four essential key-value pairs: fruit, lbs, price per lb, and in stock (boolean). To access these values efficiently, employ square bracket syntax with the object name and key: `fruit_list[0]["fruit"]` retrieves "Apple" from the first dictionary in our list.

Our objective is to create a filtered dataset through list comprehension and conditional logic. We'll iterate through our original list of dictionaries to generate `medium_priced_fruits`—a curated collection containing only fruits priced between $1.00 and $2.00 per pound that are currently available in inventory. This type of filtering operation mirrors real-world data processing scenarios where you need to extract subsets based on multiple criteria.

Additionally, we'll demonstrate dynamic key creation by calculating a `total_price` field on-the-fly. This computed field multiplies pounds by price per pound, showcasing how to enhance existing data structures with derived values—a technique particularly valuable in business analytics and financial modeling applications.

The implementation follows a straightforward algorithmic approach that prioritizes readability and maintainability. We initialize an empty `medium_price_fruits` list, then iterate through each dictionary in our source data. For each item, we apply our filtering logic: price per pound must fall within our $1-$2 range (`>= 1 and <= 2`), and inventory status must be true (`dictionary["in stock"] == True`). When both conditions are satisfied, we dynamically add the `total_price` key with our calculated value, then append the enhanced dictionary to our results list.

Upon completion, we output the `medium_price_fruits` list for verification—a critical step in any data transformation workflow. This pattern of filter-transform-validate represents a cornerstone methodology in modern data science and backend development, providing both immediate results and a foundation for more complex data pipeline operations.

Implementation Process

1

Initialize Empty List

Create a new list called medium_priced_fruits to store filtered results

2

Iterate Through Dictionary List

Loop through each fruit dictionary in the original fruit_list collection

3

Apply Price Filter

Check if price per pound is between 1 and 2 dollars inclusive

4

Check Stock Status

Verify that the in stock value is true before including the item

5

Calculate Total Price

Add new total price key by multiplying pounds times price per pound

6

Append to Results

Add the modified dictionary to the medium_priced_fruits list

Dictionary Access Pattern

Use fruit_list[0]["fruit"] syntax to access dictionary values. The first bracket specifies the list index, the second specifies the dictionary key.

Filter Criteria Checklist

0/4

Before vs After Processing

FeatureOriginal DictionaryFiltered Dictionary
Keysfruit, lbs, price per lb, in stockfruit, lbs, price per lb, in stock, total price
Price RangeAll prices$1.00 - $2.00 only
Stock StatusAll itemsIn stock only
CalculationsNoneTotal price computed
Recommended: The filtered dictionary provides more focused data with enhanced computational fields
We will also add a new key on the fly, called total price, which will be a math calculation of the pounds times the price per pound.
This demonstrates dynamic dictionary modification during processing, a powerful Python technique for data transformation.

Key Takeaways

1Dictionary filtering requires combining multiple conditional statements to achieve precise data selection criteria
2Square bracket notation provides direct access to dictionary values using both list indices and key names
3Dynamic key addition allows real-time calculation and insertion of new fields during iteration processes
4Price range filtering uses comparison operators to establish both minimum and maximum value boundaries
5Boolean field checking enables filtering based on true/false conditions like stock availability status
6List comprehension concepts apply to dictionary filtering operations for efficient data processing workflows
7Mathematical calculations can be embedded directly into dictionary creation for computed field generation
8Print statements serve as essential debugging tools for verifying filtered results and data integrity

RELATED ARTICLES