In this article we will discuss different ways to fetch the first key of a dictionary. Then we will look how to select first N keys of a dictionary in python.
Table of contents
- Get first key from a dictionary using keys() method.
- Get first key in a dictionary using iter() & next().
- Get first N keys from a Dictionary in Python.
Get first key from a dictionary using keys() method
In python, dictionary is a kind of container that stores the items in key-value pairs. It also provides a function keys(), that returns an iterable sequence of all keys in the dictionary. We will use that to fetch all keys in the dictionary and then we will select first key from that sequence i.e.
# Dictionary of string and int word_freq = { 'Hello': 56, "at": 23, 'test': 43, 'This': 78, 'Why': 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: Hello
Here we converted the iterable sequence of keys to a list and then selected the first element from the list i.e. first key of the dictionary
Method 2:
If we try to convert dictionary to list by passing the dictionary object to list constructor list(), then implicitly it will convert all keys of the dictionary to list and then we can select the first element from the list i.e. first key of the dictionary,
Frequently Asked:
- Python: Iterate over a dictionary sorted by key
- Append to a Python dictionary using a for loop
- Solved- TypeError: dict_keys object does not support indexing
- Loop / Iterate over all values of dictionary in Python
# Dictionary of string and int word_freq = { 'Hello': 56, "at": 23, 'test': 43, 'This': 78, 'Why': 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: Hello
Here we didn’t even called the keys() function. We just created a list of keys from the dictionary and selected first item from it.
Related Stuff:
- Solved- TypeError: dict_keys object does not support indexing
- Python: Get first value in a dictionary
- Get first key-value pair from a Python Dictionary
Get first key in a dictionary using iter() & next()
In in the above solution, we created a list of all the keys and then selected the first key. It was not an efficient solution, because if our dictionary is large and we need only the first key, then why are we creating a huge list of all keys. As keys() returns an iterable sequence of dictionary keys, so we can create an iterator object of this iterable sequence of keys using iter() function. Then by calling the next() function on iterator object, we can get the first element of this sequence i.e. first key of the dictionary. For example,
# Dictionary of string and int word_freq = { 'Hello': 56, "at": 23, 'test': 43, 'This': 78, 'Why': 11 } # Get the first ket in a dictionary first_key = next(iter(word_freq)) print('First Key of dictionary:') print(first_key)
Output:
First Key of dictionary: Hello
This is an efficient solution because didn’t iterated over all the keys in dictionary, we just selected the first one.
Get first N keys from a Dictionary in Python
To select first N keys from a dictionary, convert the keys of a dictionary to a list and then select first N entries from that. For example, let’s see how to select first 3 keys from a dictionary,
# Dictionary of string and int word_freq = { 'Hello': 56, "at": 23, 'test': 43, 'This': 78, 'Why': 11 } # get list of first 3 keys from the dictionary n = 3 first_n_keys = list(word_freq.keys())[:n] print(f'First {n} keys in the dictionary:') print(first_key)
Output:
First 3 keys in the dictionary: Hello
Conclusion:
We learned about different about different ways to select first key from a dictionary in python.