How to add pairs to a dict, typo corrected in this one.

Paul Rubin http
Fri Mar 12 05:01:02 EST 2004


Avik Ghose <avikghose at yahoo.co.in> writes:
> if 'tom' in phone_book.keys():
>     #duplicate entry;

Don't do it that way!  That builds up a whole list from scratch containing
all the keys, and then searches the list for 'tom'!!!  There was just
a thread about this :).

Instead, use: 

   if 'tom' in phone_book: ...

or

   if phone_book.has_key('tom'): ...



More information about the Python-list mailing list