Join us as we tackle this difficult Python programming challenge and learn a few valuable skills along the way!
Starting File
Final File
Video Transcription
Each dictionary in the list has four key-value pairs: fruit, lbs, price per lb, and in stock (true or false). To access them, use square bracket syntax and the name of the object with the key name. For example, to print Apple, you would use: `fruit_list[0]["fruit"]`.
We are going to loop through the list of dictionaries and produce a new list of dictionaries called medium_priced_fruits. This will do a filter operation, and include only those fruits that cost more than a dollar per pound but less than two dollars per pound. It will also filter out those that are not in stock.
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.
To write this, we don't need to make a function. We will have a medium_price_fruits list and then iterate through the fruit list of dictionaries. Every time, we will ask if the price per pound of the current item is greater than or equal to one, and less than or equal to two. If it is, and the item is in stock (dictionary["in stock"] is true), we will add a new key, called total price, which is the number of pounds times the price per pound. We will then append the dictionary to the medium_price_fruits list.
When done, we will print the medium_price_fruits list to check the output.