A neat way to pop up a key, the value of PAIR from a dictionary?

pop is a very small function that when used in dictionaries (taking into account a known key) removes an element with this key from the dictionary and also returns the corresponding value. But what if I want a key too?

Obviously, in simple cases, I could just do something like this:

 pair = (key, some_dict.pop(key)) 

But if, say, I would like to put a key-value pair with the smallest value, following the idea above, I would have to do this ...

 pair = (min(some_dict, key=some.get), some_dict.pop(min(some_dict, key=some_dict.get))) 

... this is disgusting since I have to perform the operation twice (obviously, I could store the output from min in a variable, but I'm still not quite happy with that). So my question is: is there an elegant way to do this? Did I miss the obvious trick here?

+7
python dictionary
source share
3 answers

You can define your dictionary using python ABC , which provides an infrastructure for defining abstract base classes . And then overload the pop attribute of python dictionary objects based on your need:

 from collections import Mapping class MyDict(Mapping): def __init__(self, *args, **kwargs): self.update(dict(*args, **kwargs)) def __setitem__(self, key, item): self.__dict__[key] = item def __getitem__(self, key): return self.__dict__[key] def __delitem__(self, key): del self.__dict__[key] def pop(self, k, d=None): return k,self.__dict__.pop(k, d) def update(self, *args, **kwargs): return self.__dict__.update(*args, **kwargs) def __iter__(self): return iter(self.__dict__) def __len__(self): return len(self.__dict__) def __repr__(self): return repr(self.__dict__) 

Demo:

 d=MyDict() d['a']=1 d['b']=5 d['c']=8 print d {'a': 1, 'c': 8, 'b': 5} print d.pop(min(d, key=d.get)) ('a', 1) print d {'c': 8, 'b': 5} 

Note. Since @chepner is suggested as the best choice in a comment, you can override a popitem that already returns a key / value pair.

+3
source share

The heap supports the pop-min function you describe. However, first you need to create a bunch from your dictionary.

 import heapq # Must be two steps; heapify modifies its argument in-place. # Reversing the key and the value because the value will actually be # the "key" in the heap. (Or rather, tuples are compared # lexicographically, so put the value in the first position.) heap = [(v, k) for k, v in some_dict.items()] heapq.heapify(heap) # Get the smallest item from the heap value, key = heapq.heappop(heap) 
+3
source share

here is a simpler implementation

 class CustomDict(dict): def pop_item(self, key): popped = {key:self[key]} #save "snapshot" of the value of key before popping self.pop(key) return popped a = CustomDict() b = {"hello":"wassup", "lol":"meh"} a.update(b) print(a.pop_item("lol")) print(a) 

So, here we create a custom dict that returns the desired item and issues a key-value pair

+1
source share

All Articles