How to extract the dictionary key? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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