#๐Ÿ”’ what is going inside the get_quantity function?

39 messages ยท Page 1 of 1 (latest)

tepid linden
#
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```
hardy sinewBOT
#

@tepid linden

Python help channel opened

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.

tepid linden
#

tuplst.sort(ket=get_quantity)

what is going inside the get_quantity(item) ???

#

what is item here?

tender mesa
#

hi @tepid linden
what the matter?

#

explain more detail

bold glade
#

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.

tepid linden
#

shouldn't it be get_quantity(list. of tuples items) ?

#

idk how to call them

bold glade
#

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.

tepid linden
#

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)

bold glade
#

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.

tepid linden
#

does it automatically know they need to compare tuples not the list

#

oh

#

list itself is tuples

#

so the key is

#

comparing

#

elements

bold glade
#

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.

tepid linden
#

get_quantity(element):
return element[1]

#

sorting by element [1]

#

i get it

bold glade
#

Yep

tepid linden
#

so default order is ascending?

#

from 0 to 9?

bold glade
#

Yes.

tepid linden
#

ok

bold glade
#

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.

tepid linden
#

so if i need to ordder dictionaries i need tochange the datatypes to list of tuples right?

bold glade
#

Well this code does kind of order a dictionary, because it makes a list of the key/value pairs from the dictionary.

tepid linden
#

yes

#

usually they give out problems where they give out dict and make me to order it

bold glade
#

You can't order a dictionary itself. (Though these days a dictionary does have an order: the order the key/value pairs were inserted.)

tepid linden
#

yeah. thank you ver much ๐Ÿ˜ญ

#

!close

hardy sinewBOT
#
Python help channel closed with !close

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.