How to get First Key in a Dictionary Python: In this tutorial, we will discuss different ways to get the first key in a dictionary. Later, we will see & learn how to choose the first N Keys of a dictionary in Python.
- Get the first key from a dictionary using keys() method
- Get first key in a dictionary using iter() & next()
- Get the First Key in Dictionary Using list() Function
- Get the First Key in Dictionary Using for Loop
- Get first N keys from a Dictionary in Python
Get the first key from a dictionary using keys() method
Dictionary stores elements in key-value pairs.Dictionary act as a container in python. Dictionary provided keys() function which we will use to fetch all keys in it and after that we select the first key from a sequence.
# Dictionary of string and int word_freq = { 'Anni': 56, "is": 23, 'my': 43, 'Fav': 78, 'Person': 11 } # Get the first key in a dictionary first_key = list(word_freq.keys())[0] print('First Key of dictionary:') print(first_key)
Output:
First Key of dictionary: Anni
In the above example, you can see that first we have fetched all dictionary elements and by using indexing we find out the first key value.
Do Refer:
Here is another way to do the same,
Another Way for How to get First Key in Dictionary Python
By using this method, it will convert all keys of the dictionary to a list and then we can select the first element from the list.
# Dictionary of string and int word_freq = { 'Anni': 56, "is": 23, 'my': 43, 'Fav': 78, 'Person': 11 } # Get the first ket in a dictionary first_key = list(word_freq)[0] print('First Key of dictionary:') print(first_key)
Output:
First Key of dictionary: Anni
In the above example, we didn’t call the keys() function. We created a list of keys from the dictionary and selected the first item from it.
Get first key in a dictionary using iter() & next()
What we have done above that was not a perfect solution because first, we created a list and then fetch the first key in a dictionary. It is very difficult to apply that method in a large number of dictionaries. First, we iterate the object of keys using the iter() function then we apply the next() function on it for getting the first element.
This is an efficient solution because didn’t iterate over all the keys in a dictionary, we just selected the first one.
# Dictionary of string and int word_freq = { 'Anni': 56, "is": 23, 'my': 43, 'Fav': 78, 'Person': 11 } # Get the first key in a dictionary first_key = next(iter(word_freq)) print('First Key of dictionary:') print(first_key)
Output:
First Key of dictionary: Anni
Get the First Key in Dictionary Using list() Function
Also, there is a possible way to convert the dict type into a list using thelist()
function at first and later get the first key at the 0th index of the dictionary.
my_dict = { 'Russia': 2, 'New York': 1, 'Lahore': 6, 'Tokyo': 11} print(list(my_dict.keys())[0])
Result:
Russia
Get the First Key in Dictionary Using for Loop
One more easiest way to get the initial key in a dictionary is using thefor
loop. After getting the first key of the dictionary break the loop.
Let’s see an example on it:
my_dict = { 'London': 2, 'New York': 1, 'Lahore': 6, 'Tokyo': 11} for key, value in my_dict.items(): print(key) break
Output:
London
Get first N keys from a Dictionary in Python
To select the first N keys from a dictionary, convert the keys of a dictionary to a list and then select the first N entries from that. For example, let’s see how to select the first 3 keys from a dictionary,
# Dictionary of string and int word_freq = { 'Anni': 56, "is": 23, 'my': 43, 'Fav': 78, 'Person': 11 } # Get the first ket in a dictionary first_key = list(word_freq)[0] print('First Key of dictionary:') print(first_key)
Output:
First Key of dictionary: Anni
Conclusion on Get First value in a dictionary of string and int
In this article, we have seen discuss different ways to find out the first key of a dictionary in python. All these dictionary keys methods in python help to find easily the key value in a dictionary of string and int word_freq. Get first key of the dictionary in python information completely from this article.