pythonfunctionperformanceerror-correctioncorrectness

How do i return more than one result from this if the input have similar value


E.g.: In this scenario, most expensive items are needed.

d= (('Shirts',40000),('trousers',40000),('provisions',34000),('others',34000))

def pick_the_most_expensive_from_my_list(d):
    material = '' 
    price = None
    for x,y in d:
        if price is None:
            price= y
            material= x
        while y > price:
            price = y                           
            material= x
        
    return material,price

Solution

  • You could find the max price first, then return pairs that have that price.

    d= (('Shirts',40000),('trousers',40000),('provisions',34000),('others',34000))
    
    def pick_the_most_expensive_from_my_list(d):
        return [pair for pair in d if pair[1]==max([item[1] for item in d])]
    

    Output

    [('Shirts', 40000), ('trousers', 40000)]