Personal tools
You are here: Home CDAT Tips and Tricks Python Tips Type dictionary in python.
Document Actions

Type dictionary in python.

by Renata McCoy last modified 2007-05-08 10:47

Goal: Learn about dictionaries in python.

 
A dictionary allows you to associate values index by keys. Dictionaries can be created using values in curly braces like this:
a = {
"name" : "CCM3",
"center" : "NCAR,"
"model_of" : "Atmosphere"
}
To access members of the dictionary, we use the key-indexing facility:
model_name = a["model"]
modelling_center = a["center"]

The keys associated with a dictionary can be obtained as a list by:

b = a.keys()

You can check the dictionary membership by using the has_key() method:

if a.has_key("model_of"):
print a["model_of"]
else:
print "Unknown model of"
NOTE:

Dictionaries have no concept of order among elements. It is incorrect to say that the elements are “out of order”; they are simply unordered.


Powered by Plone