a={'top':2,'mid':1,'bot':4,'jug':0}
Here's a dictionary called a and I'm going to get the highest value of a
But if a is a list,
MAX = 0
i =0
for x in range(len(a)):
if a[i]>MAX:
MAX = a[0]
i=i+1
else: i=i+1
That's how you get the maximumYo
How can I do it this way in dictionary?Can't you do it'
python
a = {
'top':2,'mid':1,'bot':4,'jug':0
}
for key, value in a.items():
print(key)
print(value)
When you pass a dictionary to the destination of the for-in loop, you can get the key and value of the element at every iteration.
https://programmers.co.kr/learn/courses/2/lessons/285
Simple Maximum
a={'top':2,'mid':1,'bot':4,'jug':0}
max(a.values())
# 4
Key value mapped with maximum value
a={'top':2,'mid':1,'bot':4,'jug':0}
max(a.keys(), key=a.get)
# # 'bot'
Find all key-values with maximum values
a={'top':2,'mid':1,'bot':4,'jug':0}
from operator import itemgetter
key, value = max(a.items(), key=itemgetter(1))
# # key: 'bot', value: 4
© 2024 OneMinuteCode. All rights reserved.