Python: Get First Value in a Dictionary

Dictionaries are Python’s implementation of an associative array, which is a data structure. A dictionary is made up of a set of key-value pairs. Each key-value pair corresponds to a key and its corresponding value.

A dictionary is defined by enclosing a comma-separated list of key-value pairs in curly braces { }. Each key is separated from its associated value by a colon ‘ :

Examples:

Input :

dictionary = {'This': 100, 'is':200, 'BTechGeeks':300}

Output:

100

Find the first value in a dictionary

There are several ways to find the first value in a dictionary some of them are:

Method #1:Using item() function

The view object is returned by the items() method. The key-value pairs of the dictionary are stored in the view object as tuples in a list. Any changes made to the dictionary will be reflected in the view object.

The dictionary’s item() function returns a view of all dictionary items in the form of a sequence of all key-value pairs. Choose the first key-value pair and the first value from this sequence.

Below is the implementation:

# Given Dictionary
dictionary = {'this': 100, 'is': 200, 'BTechGeeks': 300}
# converting dictionary items to list
dictlist = list(dictionary.items())
# printing the first value of this list
print(dictlist[0][1])

Output:

100

Method #2:Using list() and values() functions

To complete this task, a combination of the methods listed above can be used. In this, we just convert the entire dictionaries values extracted by values() into a list and just access the first values. There is only one thing you should keep in mind when using this, and that is its complexity. It will first convert the entire dictionary to a list by iterating over each item, after which it will extract the first element. The complexity of this method would be O(n).

Below is the implementation:

# Given Dictionary
dictionary = {'this': 100, 'is': 200, 'BTechGeeks': 300}
# converting dictionary values to list
dictlist = list(dictionary.values())
# printing the first value of this list
print(dictlist[0])

Output:

100

Method #3:Using iter() and next() functions

These functions can also be used to complete this task. In this case, we simply use next() to get the first next value, and the iter() function is used to get the iterable conversion of dictionary items. So, if you only need the first value, this method is more efficient. Its complexity would be O(1).

Below is the implementation:

# Given Dictionary
dictionary = {'this': 100, 'is': 200, 'BTechGeeks': 300}
# Get first value of dictionary using iter
firstval = next(iter(dictionary.items()))[1]
# printing the first value
print(firstval)

Output:

100

Retrieve the first N values from a Python dictionary

Using list() + values() +slicing:

Convert the dictionary values into a list.

Retrieve the first n values of the dictionary using slicing.

Below is the implementation:

# Given Dictionary
dictionary = {'this': 100, 'is': 200, 'BTechGeeks': 300}
# Given n
n = 2
# converting dictionary values to list
dictvalues = list(dictionary.values())
# print all values upto n using slicing
print(*dictvalues[:n])

Output:

100 200

Related Programs: