I have a Python list called “products”
I am attempting to sort it by price (cheapest first), but I can not figure out how to reference the values after the $ to begin sorting.
With a list such as below, can some provide guidance?
I understand that “,”s between each items properties would be best, its something I cant generate prior to this output of the products list.
sorted_products = sorted(products, key=lambda x: (float(x['$'])))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
TypeError: 'Product' object is not subscriptable
Most online references I find are referencing each element broken up individually to use as a key, which I can not do.
List:
[(Dickies) Dickies Men’s Heavyweight Short Sleeve T-Shirt - Black - m: $30, (Dickies) Dickies Men’s Heavyweight Short Sleeve T-Shirt - Hunter Green - l: $30, (Dickies) Dickies Men’s Heavyweight Short Sleeve T-Shirt - Black - 2xl tall: $32, (Dickies) Dickies Men’s Heavyweight Short Sleeve T-Shirt - Charcoal - l: $30, (Dickies) Dickies Men’s Heavyweight Short Sleeve T-Shirt - Hunter Green - xl tall: $32, (Dickies) Dickies Men’s Heavyweight Short Sleeve T-Shirt - Black - 2xl: $30, (Dickies) Dickies Men’s Heavyweight Short Sleeve T-Shirt - Heather Gray - xl: $30, (Dickies) Dickies Men’s Heavyweight Short Sleeve T-Shirt - Black - l: $30, (Dickies) Dickies Men’s Heavyweight Short Sleeve T-Shirt - Black - xl: $30, (Dickies) Dickies Men’s Heavyweight Short Sleeve T-Shirt - Heather Gray - l: $30]