get first key value pair in dictionary python code example
Example 1: python return first n key values pairs from dictionary
{key: dictionary[key] for key in list(dictionary)[:number_keys]}
dictionary = {'a': 3, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
{key: dictionary[key] for key in list(dictionary)[:2]}
--> {'a': 3, 'b': 2}
dictionary = {'a': 3, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
{key: dictionary[key] for key in list(dictionary)[2:5]}
--> {'c': 3, 'd': 4, 'e': 5}
Example 2: python get first value in a dictionary
word_freq = {
'Hello' : 56,
"try" : 23,
'test' : 43,
'This' : 78,
'Way' : 11
}
first_value = list(word_freq.values())[0]
print('First Value: ', first_value)