container-indepentent iteration code ?

Terry Reedy tjreedy at udel.edu
Thu Sep 9 02:04:10 EDT 2004


"flacco" <flacco002 at spambadTwilight-systems.com> wrote in message 
news:10jv9ae988bbnb7 at corp.supernews.com...
> Jeremy Bowers wrote:
>> On Wed, 08 Sep 2004 19:27:38 -0400, flacco wrote:
>>
>>>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.
>>
>> In what way does
>>
>> for obj in container:
>>
>> not meet your needs?
>
> i always want obj to be the value.  dicts, for example, yield keys 
> instead of values (i think?)...

When iterators were introduced, there was discussion of whether

for x in somedict:

should iterate over dict.keys(), dict.values(), dict.items(), or continue 
to be illegal.  dict.keys() won as being most useful because most commonly 
needed.  Iterating over values or items continues to have to be explicit.

Terry J. Reedy






More information about the Python-list mailing list