Get first element of dict python

Update: as of Python 3.7, insertion order is maintained, so you don't need an OrderedDict here. You can use the below approaches with a normal dict

Changed in version 3.7: Dictionary order is guaranteed to be insertion order. This behavior was an implementation detail of CPython from 3.6.

source

Python 3.6 and earlier*

If you are talking about a regular dict, then the "first key" doesn't mean anything. The keys are not ordered in any way you can depend on. If you iterate over your dict you will likely not get "banana" as the first thing you see.

If you need to keep things in order, then you have to use an OrderedDict and not just a plain dictionary.

import collections
prices  = collections.OrderedDict[[
        ["banana", 4],
        ["apple", 2],
        ["orange", 1.5],
        ["pear", 3],
]]

If you then wanted to see all the keys in order you could do so by iterating through it

for k in prices:
    print[k]

You could, alternatively put all of the keys into a list and then work with that

keys = list[prices]
print[keys[0]] # will print "banana"

A faster way to get the first element without creating a list would be to call next on the iterator. This doesn't generalize nicely when trying to get the nth element though

>>> next[iter[prices]]
'banana'

* CPython had guaranteed insertion order as an implementation detail in 3.6.

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.

Advertisements

Best FREE courses to learn Python

Python is one of the most used programming language in 2021.

Learn from the best today.

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:

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.

How will you get the first value in a dictionary in Python?

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.

How do you get the first key from a dict in Python?

Use next[] to get the first key value in a dictionary.
Call dict. ... .
Call iter[object] with the view object from step 1 as object to return an iterator of the values..
Call next[iterator] with the iterator from step 2 as iterator to return the first value from the iterator..

How do you find the first element in a dictionary?

Dictionary d = new Dictionary[] { {1,"Electronics"}, {2, "Clothing"}, {3,"Toys"}, {4,"Footwear"}, {5, "Accessories"} }; Now to display the first element, set the key like this.

How do I find the first key value pair in a dictionary?

In Python, there are a few different ways we can get the first key/value pair of a dictionary. The easiest way is to use the items[] function, convert it to a list, and access the first element. If you only care about getting the first value of a dictionary, you can use the dictionary values[] function.

Chủ Đề