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
Initialize Empty List
Create a new list called medium_priced_fruits to store filtered results
Iterate Through Dictionary List
Loop through each fruit dictionary in the original fruit_list collection
Apply Price Filter
Check if price per pound is between 1 and 2 dollars inclusive
Check Stock Status
Verify that the in stock value is true before including the item
Calculate Total Price
Add new total price key by multiplying pounds times price per pound
Append to Results
Add the modified dictionary to the medium_priced_fruits list
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
Ensures minimum price threshold for medium-priced category
Maintains upper limit for medium-priced classification
Filters out unavailable items from the results
Add computed field showing total cost for the quantity
Before vs After Processing
| Feature | Original Dictionary | Filtered Dictionary |
|---|---|---|
| Keys | fruit, lbs, price per lb, in stock | fruit, lbs, price per lb, in stock, total price |
| Price Range | All prices | $1.00 - $2.00 only |
| Stock Status | All items | In stock only |
| Calculations | None | Total price computed |
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.