[Tutor] Is there a programmatic use for keys() and values()

Chris “Kwpolska” Warrick kwpolska at gmail.com
Sat Jun 15 20:51:22 CEST 2013


On Sat, Jun 15, 2013 at 8:41 PM, Jim Mooney <cybervigilante at gmail.com> wrote:
> When I try to get the keys of a dictionary, such as d.keys(), I get
> the below instead of a plain list, and it's not very usable. How can I
> use the keys from this like it was a list, or is this basically
> useless other than to see the keys or values?
>
> *** Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC
> v.1600 32 bit (Intel)] on win32. ***
>>>> d.keys()
> dict_keys(['alpha', 'olf', 'bog', 'dog'])

The standard use is:

    for k, v in d.items():
       do_stuff_with_dict_items_here()

You can use the .keys() and .values() in a similar way.  You don’t
need a list 99% of the time.  And if you do, it is not hard to make it
one.

--
Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail                | always bottom-post
http://asciiribbon.org        | http://caliburn.nl/topposting.html


More information about the Tutor mailing list