I am trying to create a function that takes a item with the price and then organise it by price. I have found a way to organise this, but I cannot find a way to select the limit of the highest price if that makes sense, E.G if I want to put limit to two, I only want the program to find the two biggest items, the below is what i have so far.
def bigger_price(limit: int, data: list[dict]) -> list[dict]:
price_sorted = sorted(data, key = lambda row: (row["price"]))
a = 0
while a < limit:
for i in reversed(price_sorted):
a += 1
return i
print("Example:")
print(
bigger_price(
2,
[
{"name": "bread", "price": 100},
{"name": "wine", "price": 138},
{"name": "meat", "price": 15},
{"name": "water", "price": 1},
],
)
)
assert bigger_price(
2,
[
{"name": "bread", "price": 100},
{"name": "wine", "price": 138},
{"name": "meat", "price": 15},
{"name": "water", "price": 1},
],
) == [{"name": "wine", "price": 138}, {"name": "bread", "price": 100}]
assert bigger_price(
1, [{"name": "pen", "price": 5}, {"name": "whiteboard", "price": 170}]
) == [{"name": "whiteboard", "price": 170}]
print("The mission is done! Click 'Check Solution' to earn rewards!")
I am unsure why my code is not working as intending when trying to organise a dictionary via price order.