container-indepentent iteration code ?

Raymond Hettinger vze4rx4y at
Thu Sep 9 06:45:47 CEST 2004

> is there a way to iterate over the *values* in a list/dict/whatever,
> regardless of whether it's a list, dict, or whatever?  ie, the iteration
> code will not know beforehand what kind of container it's getting.

    it = obj.itervalues()
except AttributeError:
    it = iter(obj)
for value in it:
    . . .

Raymond Hettinger

More information about the Python-list mailing list