How to print a dictionary's key?

前端 未结 20 809
眼角桃花
眼角桃花 2020-11-27 09:22

I would like to print a specific Python dictionary key:

mydic = {}
mydic[\'key_name\'] = \'value_name\'

Now I can check if mydic.has_

20条回答
  •  遥遥无期
    2020-11-27 09:57

    dic = {"key 1":"value 1","key b":"value b"}
    
    #print the keys:
    for key in dic:
        print key
    
    #print the values:
    for value in dic.itervalues():
        print value
    
    #print key and values
    for key, value in dic.iteritems():
        print key, value
    

    Note:In Python 3, dic.iteritems() was renamed as dic.items()

提交回复
热议问题