How to extract the dictionary key? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to extract the dictionary key?

def mostMembers(x):     list=[]     print("[what alphabet has the biggest number]")     for value in sorted(x.values()): #name?         list.append(value)     for i in list:         if list[0]<list[i]:             list[0]=list[i]         else:             pass     return list[i] member={'a':5,'b':0,'c':4,'d':7,'e':2,'f':3,'g':0,'h':2} print(mostMembers(member)) I can see the value but I want to get key.

30th May 2018, 1:30 PM
park
3 Answers
+ 3
Here, look at this. It does no sorting. It just iterates over the dict directly. Now, let me know whether or not it solves your issue. https://code.sololearn.com/cFGJqDQZg773/?ref=app
30th May 2018, 1:52 PM
cyk
cyk - avatar
+ 1
thank you so much :) I will try it again!
30th May 2018, 1:57 PM
park
+ 1
mydict = { 1: “one”, 2: “two” } keys = mydict.keys() values = mydict.values() # get the keys or values for (k, v) in mydict.items(): print(k, v) # loops key:value pairs
30th May 2018, 6:03 PM
Pedro Demingos
Pedro Demingos - avatar