[Python-3000] Iterators for dict keys, values, and items == annoying :)

skip at pobox.com skip at pobox.com
Thu Mar 23 22:12:47 CET 2006


    Guido> It's interesting to me because there's a bunch of APIs that
    Guido> currently have two versions: one to get a list and one to get an
    Guido> iterator. It would be cleaner if only the iterator version
    Guido> existed, and the way to get a list was to put an explicit list()
    Guido> around it. Building the list is expensive, and often not needed
    Guido> (a lot of algorithms don't mutate the dict).

Agreed.  I think there's also a perceived performance difference, whether
one exists or not.  I'd be real surprised if

    for key, val in somedict.iteritems():
        blah(key, val)

was faster than 

    for key, val in somedict.items():
        blah(key, val)

for small dicts.  Still, around work I see a great preference for the longer
(and uglier IMO) spelling.  Maybe it's a mental carryover from C++ that
makes people what that version?

In any case, I vote to get rid of iterBLAH in favor of just BLAH, and in
most cases make BLAH() return an iterator (or a view I suppose), with
explicit list(), tuple(), set() required to get various concrete containers.

Skip


More information about the Python-3000 mailing list