container-indepentent iteration code ?

Raymond Hettinger vze4rx4y at verizon.net
Thu Sep 9 00:45:47 EDT 2004


[flacco]
> 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.

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


Raymond Hettinger






More information about the Python-list mailing list