Iterating over the data items in a dict. 
Author Message
 Iterating over the data items in a dict.

I would like to iterate over the data items, not the keys, in a dict.
The closest I can get is:

        for n in mydict.keys():
            i = mydict[n]
            do_something_useful(i)

However, this seems a bit awkward.

--
Big Gaute                               http://www.*-*-*.com/ ~gs234/
Yow!  Are we wet yet?



Fri, 25 Apr 2003 03:00:00 GMT  
 Iterating over the data items in a dict.

Quote:

>I would like to iterate over the data items, not the keys, in a dict.
>The closest I can get is:

>        for n in mydict.keys():
>            i = mydict[n]
>            do_something_useful(i)

>However, this seems a bit awkward.

Use mydict.values()

--
Dale Strickland-Clark
Out-Think Ltd
Business Technology Consultants



Fri, 25 Apr 2003 03:00:00 GMT  
 Iterating over the data items in a dict.



Quote:

> I would like to iterate over the data items, not the keys, in a dict.

I think you can do it like this:

for key, value in dict.items:
    # do stuff



Fri, 25 Apr 2003 03:00:00 GMT  
 Iterating over the data items in a dict.

Quote:

> I think you can do it like this:

> for key, value in dict.items:

for key, value in dict.items(): # it is a function - call it!

Oleg.
----

           Programmers don't die, they just GOSUB without RETURN.



Fri, 25 Apr 2003 03:00:00 GMT  
 Iterating over the data items in a dict.

Quote:

> I would like to iterate over the data items, not the keys, in a dict.
> The closest I can get is:

>         for n in mydict.keys():
>             i = mydict[n]
>             do_something_useful(i)

> However, this seems a bit awkward.

See the .values() and .items() methods in the documentation.
--

This is a signature anti-virus.
Please stop the spread of signature viruses!


Sat, 26 Apr 2003 21:49:20 GMT  
 Iterating over the data items in a dict.

Quote:


> >I would like to iterate over the data items, not the keys, in a dict.
> >The closest I can get is:

> >        for n in mydict.keys():
> >            i = mydict[n]
> >            do_something_useful(i)

> >However, this seems a bit awkward.

> Use mydict.values()

Thanks!  It's just what I was looking for.

--
Big Gaute                               http://www.*-*-*.com/ ~gs234/
They collapsed....  like nuns in the street...
 they had no {*filter*} appeal!



Sat, 26 Apr 2003 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Iterating order of dict.items()

2. NewBuiltins: added dict() -- inverse of dict.items()

3. Iterating over items in the registry

4. Data Dict. - Open In Current Thread

5. groups in CW data dict?

6. expanding a list and dict to a *args and **dict

7. Dict can't update with dict-like objects

8. Inheriting (subclass) of dict data attribute (copying)

9. Builtin dict should be callable, since a dict defines a funct ion

10. Builtin dict should be callable, since a dict defines a function

11. Persistent db conn and fetchall() data with dict

12. Dict to String and String to Dict with Visual Works

 

 
Powered by phpBB® Forum Software