How to print a dictionary's key?

前端 未结 20 723
眼角桃花
眼角桃花 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:35

    A dictionary has, by definition, an arbitrary number of keys. There is no "the key". You have the keys() method, which gives you a python list of all the keys, and you have the iteritems() method, which returns key-value pairs, so

    for key, value in mydic.iteritems() :
        print key, value
    

    Python 3 version:

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

    So you have a handle on the keys, but they only really mean sense if coupled to a value. I hope I have understood your question.

    0 讨论(0)
  • 2020-11-27 09:35

    Or you can do it that manner:

    for key in my_dict:
         print key, my_dict[key]
    
    0 讨论(0)
  • 2020-11-27 09:35
    # highlighting how to use a named variable within a string:
    mapping = {'a': 1, 'b': 2}
    
    # simple method:
    print(f'a: {mapping["a"]}')
    print(f'b: {mapping["b"]}')
    
    # programmatic method:
    for key, value in mapping.items():
        print(f'{key}: {value}')
    
    # yields:
    # a 1
    # b 2
    
    # using list comprehension
    print('\n'.join(f'{key}: {value}' for key, value in dict.items()))
    
    
    # yields:
    # a: 1
    # b: 2
    

    Edit: Updated for python 3's f-strings...

    0 讨论(0)
  • 2020-11-27 09:36

    Additionally you can use....

    print(dictionary.items()) #prints keys and values
    print(dictionary.keys()) #prints keys
    print(dictionary.values()) #prints values
    
    0 讨论(0)
  • 2020-11-27 09:37

    In Python 3:

    # A simple dictionary
    x = {'X':"yes", 'Y':"no", 'Z':"ok"}
    
    # To print a specific key (for example key at index 1)
    print([key for key in x.keys()][1])
    
    # To print a specific value (for example value at index 1)
    print([value for value in x.values()][1])
    
    # To print a pair of a key with its value (for example pair at index 2)
    print(([key for key in x.keys()][2], [value for value in x.values()][2]))
    
    # To print a key and a different value (for example key at index 0 and value at index 1)
    print(([key for key in x.keys()][0], [value for value in x.values()][1]))
    
    # To print all keys and values concatenated together
    print(''.join(str(key) + '' + str(value) for key, value in x.items()))
    
    # To print all keys and values separated by commas
    print(', '.join(str(key) + ', ' + str(value) for key, value in x.items()))
    
    # To print all pairs of (key, value) one at a time
    for e in range(len(x)):
        print(([key for key in x.keys()][e], [value for value in x.values()][e]))
    
    # To print all pairs (key, value) in a tuple
    print(tuple(([key for key in x.keys()][i], [value for value in x.values()][i]) for i in range(len(x))))
    
    0 讨论(0)
  • 2020-11-27 09:37

    What's wrong with using 'key_name' instead, even if it is a variable?

    0 讨论(0)
提交回复
热议问题