I have a dictionary that looks like this
MyCount= {u'10': 1, u'1': 2, u'3': 2, u'2': 2, u'5': 2, u'4': 2, u'7': 2, u'6': 2, u'9': 2, u'8': 2}
I need highest key which is 10 but i if try max(MyCount.keys()) it gives 9 as highest. Same for max(MyCount).
max(MyCount.keys())
max(MyCount)
The dictionary is created dynamically.
This is because u'9' > u'10', since they are strings.
u'9' > u'10'
To compare numerically, use int as a key.
int
max(MyCount, key=int)
(Calling .keys() is usually unnecessary)
.keys()
1.4m articles
1.4m replys
5 comments
57.0k users