Home > Blockchain >  how to find max value in list?
how to find max value in list?

Time:09-08

I have a list of the dictionary:

 my_list = [
    {
     'name': 'Angela',
     'bid': 130
    },
    {
    'name': 'James',
    'bid': 145
    }, 
    {
    'name': 'Jane',
    'bid': 115
    }
    ]

What I would like to be able to do is return the maximum value of capacity and print(f"The Winner is {name} with a bid of {bid}")

CodePudding user response:

you can use max function here

my_list = [{'name': 'Angela','bid': 130},{'name': 'James','bid': 145},{'name': 'Jane','bid': 115}]
max_data = max(my_list, key= lambda x:x['bid'])
name, bid = max_data['name'], max_data['bid']
print(f"The Winner is {name} with a bid of {bid}")

output

The Winner is James with a bid of 145

CodePudding user response:

max(my_list, key=lambda x:x['bid'])

CodePudding user response:

Using format method so it looks up the keys for me:

winner = max(my_list, key=lambda x: x['bid'])
print("The Winner is {name} with a bid of {bid}".format(**winner))

Try it online!

  • Related