Another dict method...

Magnus Lie Hetland mlh at idi.ntnu.no
Fri May 18 21:57:58 EDT 2001


Anyone who sees the use of a dict.peekitem() which works
like dict.popitem() but doesn't actually remove an item
from the dictionary? Could be useful for things like
this (cf. earlier post about minimum of dict):

def key_with_minimum_value(dict):
    k1, v1 = dict.peekitem()
    for k2, v2 in dict.iteritems():
        if v2 < v1:
            k1, v1 = k2, v2
    return k1

Just a thought. The following seems a bit ... odd:

    k1, v1 = dict.popitem() # Get an item
    dict[k1] = v1           # Put it back

--

  Magnus Lie Hetland         http://www.hetland.org

 "Reality is that which, when you stop believing in
  it, doesn't go away."           -- Philip K. Dick






More information about the Python-list mailing list