get first element of dictionary python code example
Example 1: first position dict python
my_dict = {'foo': 'bar', 'spam': 'eggs'}
next(iter(my_dict))
my_dict = {'foo': 'bar', 'spam': 'eggs'}
list(my_dict.keys())[0]
Example 2: print first dictionary keys python
first_key = list(my_dict.keys())[0]
print(first_key)
Example 3: access first element of dictionary python
res = next(iter(test_dict))
Example 4: 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)
Example 5: python get first element in a list
test_list = [1, 5, 6, 7, 4]
print ("The original list is : " + str(test_list))
res = [ test_list[0], test_list[-1] ]
print ("The first and last element of list are : " + str(res))