-2

I have a dictionary like this:

header= {'f1': 13, 'f2': 7, 'f3': 45};

As you see, header['f2'] = 7 has the minimum value and this item is the second in header (its index is 1).

What I do?

I have try this code to get the index of minimum item in header (here 1) but it returns the key value:

index = min(header)

Output:

f2

What I want?

I want to get the index of the minimum item in the dictionary, How can i do that?

Saeed
  • 3,294
  • 5
  • 35
  • 52
  • What would the index be in your example? – Scott Hunter Nov 15 '18 at 19:45
  • 3
    Did you meant the `key`? There's no `index` for `dict`. – r.ook Nov 15 '18 at 19:47
  • I want something like an index in dictionary, not a key value – Saeed Nov 15 '18 at 19:53
  • 1
    I have no idea what you expect, as a plain dict does not *have* a deterministic index. The entries are hashed; the order changes sometimes as the set of keys changes. In Python 3.5.4, the `7` is the first value in the dict. – Prune Nov 15 '18 at 19:55
  • 2
    Dictionary are not ordered in Python, not until version 3.7 (and that is based on insertion order). Hence there are no index, and index is not a reliable way to make use of the `dict` either. What are you trying to accomplish exactly? – r.ook Nov 15 '18 at 19:56

3 Answers3

1

Dictionaries do not have indices

Even in Python 3.6+ (officially 3.7+), where dictionaries are insertion ordered, you cannot extract a key or value directly by position. The same is true for collections.OrderedDict. See also: Accessing dictionary items by position in Python 3.6+ efficiently.

min + enumerate

Assuming Python 3.6+, you can extract the position of a key / value based on insertion ordering via iteration. In this case, you can use min with a custom lambda function:

header = {'f1': 13, 'f2': 7, 'f3': 45}

min_idx, (min_key, min_val) = min(enumerate(header.items()), key=lambda x: x[1][1])

print((min_idx, min_key, min_val))

(1, 'f2', 7)
jpp
  • 159,742
  • 34
  • 281
  • 339
  • I get that this answers the spirit of the question, but what possible use could the position of the key be to OP though? – r.ook Nov 15 '18 at 20:03
  • @Idlehands, I cannot comment on how the dictionary is constructed. I can *imagine* if it's a static dictionary that's created from some input data and never amended, it can represent an ordered mapping. My point is in 3.7+ people can rely on insertion ordering, even if it's a very inefficient O(*n*) to use it for indexing. – jpp Nov 15 '18 at 20:05
  • 1
    @jpp I understand, I just can't help but feel this might be an XY problem. If OP could elaborate on the question perhaps we could actually point them to the right direction instead of an inefficient solution. But I digress, in the end this is what OP finds useful. – r.ook Nov 15 '18 at 20:07
0

Using header.values() returns a list of just the values of your dictionary:

>>> header= {'f1': 13, 'f2': 7, 'f3': 45}
>>> header.values()
[13, 7, 45]
>>> print(min(header.values()))
7

EDIT: Sorry, you wanted the corresponding key. Here's one way to do it, without having to include any other special libraries:

print(header.keys()[header.values().index(min(header.values()))])

Although dictionaries are not ordered in Python, lists are, and the lists you get from .keys() and .values() line up with each other.

Bill M.
  • 1,388
  • 1
  • 8
  • 16
-1

list(header.values()).index(min(header.values()))

This returns the index of the smallest value in the dict.

sowlosc
  • 470
  • 3
  • 8