0

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.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • As soon as you do `return i`, that function is done. You won't ever continue on to return any others. Perhaps you wanted to use `yield`. And there is a `reversed` parameter to `sorted`l you don't have to do that separately. – Tim Roberts Feb 21 '23 at 19:49
  • Does this answer your question? [Understanding slicing](https://stackoverflow.com/questions/509211/understanding-slicing) – JonSG Feb 21 '23 at 19:50
  • Does this answer your question? [How to sort a list of objects based on an attribute of the objects in descending order?](https://stackoverflow.com/questions/403421/how-to-sort-a-list-of-objects-based-on-an-attribute-of-the-objects-in-descending) – hc_dev Feb 21 '23 at 20:08
  • The two key concepts to apply are `sort(data, key, reversed = True)` for *sorting reversed* (by price descending) and `sorted_by_price_descending[:limit]` for *slicing* (top n) - find both questions already answered here on SO. – hc_dev Feb 21 '23 at 20:12

1 Answers1

2

Your intention is that the while loop will return the ith item from the top and run the loop equal to the limit. However, the return statement will return and then stop running the function, so the loop can ever only run through its first iteration and that's it.

Here's the alternative. First, let's sort the list from most expensive to least expensive, we can do this by adding in the reverse argument.

price_sorted = sorted(data, key = lambda row: (row["price"], reverse = True))

Then, we can return the first n items by slicing the list with n equal to the limit with return price_sorted[0:limit].

Michael Cao
  • 2,278
  • 1
  • 1
  • 13
  • please can you explain this further as I am a beginner and do not understand this? – DannyMoham1 Feb 21 '23 at 19:59
  • Apologies, I have made an attempt at a more detailed answer. Let me know if you have any more specific questions where I can explain further. – Michael Cao Feb 21 '23 at 20:32