[Python-Dev] non-mutating 'choose' to go with 'dict.popitem'?

Thomas Wouters thomas@xs4all.net
Tue, 8 May 2001 13:36:38 +0200


On Sun, May 06, 2001 at 02:15:57PM -0400, Tim Peters wrote:

> Given the new dict iterators in 2.2, there's an easier fast way that doesn't
> mutate the dict even under the covers:

> def arb(dict):
>     if dict:
>         return dict.iteritems().next()
>     raise KeyError("arb passed an empty dict")

You probably want:

arb = dict.iteritems().next

so that you don't keep on returning the same key,value pair.

-- 
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!