i am now learning list comprehensions, and want to replace a lengthy if statement with an elegant list comprehension. The following if statement is what I want to convert to comprehension list below it. The comprehension list doesn't do what I want to do yet, but atleast you can see where I am trying to go with it.
would like the list comprehension to only give back one value as how the if statement will.
Thank you in advance
weight_kg = 8
if weight_kg <= 0.25:
price_weight = 2.18
elif weight_kg <= 0.5:
price_weight = 2.32
elif weight_kg <= 1:
price_weight = 2.49
elif weight_kg <= 1.5:
price_weight = 2.65
elif weight_kg <= 2:
price_weight = 2.90
elif weight_kg <= 3:
price_weight = 4.14
elif weight_kg <= 4:
price_weight = 4.53
elif weight_kg <= 5:
price_weight = 4.62
elif weight_kg <= 6:
price_weight = 5.28
elif weight_kg <= 7:
price_weight = 5.28
elif weight_kg <= 8:
price_weight = 5.42
elif weight_kg <= 9:
price_weight = 5.42
elif weight_kg <= 10:
price_weight = 5.42
elif weight_kg <= 11:
price_weight = 5.43
else:
price_weight = 5.63
print(price_weight)
shipping_price = [{"weight": 0.25, "price" : 2.18}, {"weight": 0.5 "price" : 2.32}, {"weight": 1 "price" : 2.49}]
toy_weight = 0.6
price = [ship_price["weight"] for ship_price in shipping_price if ship_price["weight"] <= toy_weight]
print(price)