New course! Every coder should learn Generative AI!
Try a free lesson+ 5
print(max(users, key=lambda x: x['salary'])['name'])
Explanation:
You can define HOW a list is sorted or a max found by giving a key.
This key has to be a function, that returns for each item what you want to compare, in this case the salary.
So the max will be the dictionary jessica.
And from that, you access the name.
+ 6
From my view you should not use individual dicts to store information. you can try this:
names = {'mike': 10_000, 'harvey': 15_000, 'jessica': 20_000 }
sal_max = 0
for name, salary in names.items():
if salary > sal_max:
tmp_name, sal_max = name, salary
print(f'Max salary: {tmp_name}: {sal_max}')
+ 3
Lothar, riffing on your idea:
names = {'mike': 10_000, 'harvey': 15_000, 'jessica': 20_000 }
print(names[max(names, key=lambda name: names[name])])
Starting to look messy though. 🤣
+ 1
Aaah, okay!
You write a @, and then you should already see the list of people who commented!
(If not, start to write the name.)
0
How do you mean that, what comment text?