In this article we will discuss different ways to fetch the first value of a dictionary. Then we will look how to select first N values of a dictionary in python.
Table of Contents:
- Get first value in a python dictionary using values() method.
- Get first value in a python dictionary using item().
- Get first value in a python dictionary using iter() & next().
- Get first N values from a python dictionary.
Get first value in a python dictionary using values() method
In python, dictionary is a kind of container that stores the items in key-value pairs. It also provides a function values() that returns an iterable sequence of all values in the dictionary. We will use that to fetch all values in the dictionary and then we will select first value from that sequence i.e.
# Dictionary of string and int word_freq = { 'Hello' : 56, "at" : 23, 'test' : 43, 'This' : 78, 'Why' : 11 } # Get first value from dictionary first_value = list(word_freq.values())[0] print('First Value: ', first_value)
Output:
First Value: 56
Here we converted the iterable sequence of values to a list and then selected the first element from the list i.e. first value of the dictionary.
Get first value in a dictionary using item()
item() function of dictionary returns a view of all dictionary in form a sequence of all key-value pairs. From this sequence select the first key-value pair and from that select first value.
# Dictionary of string and int word_freq = { 'Hello' : 56, "at" : 23, 'test' : 43, 'This' : 78, 'Why' : 11 } # Get first value of dictionary first_value = list(word_freq.items())[0][1] print('First Value: ', first_value)
Output:
Frequently Asked:
- Add to a value in a Dictionary in Python
- Python – Dictionary
- Python: Loop / Iterate over all keys of Dictionary
- Python: Check if value exists in list of dictionaries
First Value: 56
Related Stuff:
- Solved- TypeError: dict_keys object does not support indexing
- How to get first key in Dictionary – Python
- Get first key-value pair from a Python Dictionary
Get first value in a dictionary using iter() & next()
In in the above solution, we created a list of all the values 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 items() returns an iterable sequence of dictionary keys, so we can create an iterator object of this iterable sequence of key-value pairs 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-value pair of the dictionary. Then select value from the first pair. For example,
# Dictionary of string and int word_freq = { 'Hello' : 56, "at" : 23, 'test' : 43, 'This' : 78, 'Why' : 11 } # Get first value of dictionary first_value = next(iter(word_freq.items()))[1] print('First Value: ', first_value)
Output:
First Value: 56
This is an efficient solution because didn’t iterated over all the keys in dictionary, we just selected the first one.
Get first N values from a python dictionary
Fetch all values of a dictionary as a list and then select first N entries from it. For example,
# Dictionary of string and int word_freq = { 'Hello' : 56, "at" : 23, 'test' : 43, 'This' : 78, 'Why' : 11 } n = 3 # Get first 3 values of dictionary first_n_values = list(word_freq.values())[:n] print('First 3 Values:') print(first_n_values)
Output:
First 3 Values: [56, 23, 43]
We selected first three values from the dictionary.
Conclusion:
We learned about different ways to select first value from a dictionary in python. Then we also looked at the procedure to select first N Values from a dictionary in python.