0

I have the below dictionary :

{'ram': ('math', 21), 'madhu': ('phy', 22), 'shyam': ('chem', 23)}

and I want to compare the values of dictionary with User input, but when I say m.keys(), it gives output as:

dict_keys(['ram', 'madhu', 'shyam'])

To compare with user input, how do I print the keys as keywords only like ram,madhu,shyam, instead of printing dict_keys(['ram', 'madhu', 'shyam'])?

fhdrsdg
  • 10,297
  • 2
  • 41
  • 62

3 Answers3

0

In order to have a list, use:

m = {'ram': ('math', 21), 'madhu': ('phy', 22), 'shyam': ('chem', 23)

for key in list(m.keys()):
     print(key) #etc
Attersson
  • 4,755
  • 1
  • 15
  • 29
0

You can iterate over the keys like as:

 for key, value in dict1.items():
     print(key)

Output:

 ram
 shyam
 madhu
0

You can do so:

d = {'ram': ('math', 21), 'madhu': ('phy', 22), 'shyam': ('chem', 23)}
list_d = list(d)
print list_d

Output:

['ram', 'shyam', 'madhu']
Joe
  • 12,057
  • 5
  • 39
  • 55