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.