def get_quantity(item):
return item[1]
def summarize_sales(lst):
total = {}
for product, quantity in lst:
total[product] = total.get(product, 0) + quantity
# Create list of (product, quantity) tuples and sort by quantity
tuplst = []
for product, quantity in total.items():
tuplst.append((product, quantity))
# Sort by quantity (second element of tuple) in descending order
tuplst.sort(key=get_quantity, reverse=True)
# Print sorted products
for product, quantity in tuplst:
print(f"{product}: {quantity}")
return totals```
#๐ what is going inside the get_quantity function?
39 messages ยท Page 1 of 1 (latest)
@tepid linden
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
tuplst.sort(ket=get_quantity)
what is going inside the get_quantity(item) ???
what is item here?
item is one of your (product,quantity) tuples. Tuples are essentially read-only lists - you can't change them after you've made them.
Anyway, item is a (product,quantity) 2-tuple, and item[0] is the product and item[1] is the quantity.
You've made a list called tuplst, your list of 2-tuples.
You''ve asked to sort it:
tuplst.sort(key=get_quantity, reverse=True)
This specifies how to sort it. The reverse=True says "backwards" - highest to lowest.
The key= specifies a function used to compute the value which is used to compare each tuple.
It is called for each tuple, so it gets an individual (product,quantity) 2-tuple. It returns the quantity part of that, which means that the sort compares the tuples by their quanity.
i get key is using the get_quantity function. but how does key know which one to put in there?
it's actually list.sort(key=get_quantity)
The sort function hands tuples to the function when it has to compare 2 tuples when sorting them. So that's when it calls the function, for each of the 2 tuples it's comparing.
does it automatically know they need to compare tuples not the list
oh
list itself is tuples
so the key is
comparing
elements
Yes. That's how it knows what it's comparing. But the function specifies how to compare them. Supposing you wanted to sort on the product? You'd use a different function, but you'd be sorting the same tuples.
Yep
Yes.
ok
And tuples do have a "natural" order: compare the first elements of the tuple. If they're the same, compare the next elements and so on
So you can just tuplst.sort(), but you'd get things in asending order by product and then quantity.
so if i need to ordder dictionaries i need tochange the datatypes to list of tuples right?
Well this code does kind of order a dictionary, because it makes a list of the key/value pairs from the dictionary.
yes
usually they give out problems where they give out dict and make me to order it
You can't order a dictionary itself. (Though these days a dictionary does have an order: the order the key/value pairs were inserted.)
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.