How can i retrieve a key from a dictionary?

Nils Kassube nika at kassube.de
Sun Nov 4 07:14:10 EST 2001


Husam <h.jehadalwan at student.kun.nl> writes:

> dict.keys() will gets all the keys. but, i need to select explicitly one
> key for printing along its value.

>>> dict={}
>>> dict[1]="Foo"
>>> print dict[1]
Foo

This dictionary method is also useful:

>>> print {}.get.__doc__
D.get(k[,d]) -> D[k] if D.has_key(k), else d.  d defaults to None.

i.e. to have a default value of 42 for the key 2 which is used
when an entry for 2 is missing: 

>>> print dict.get(2, 42)
42

Please read the tutorial before asking any other question. 
Danke. 



More information about the Python-list mailing list