Building and accessing an array of dictionaries
Jussi Piitulainen
jpiitula at ling.helsinki.fi
Thu Jan 16 04:52:12 EST 2014
Sam writes:
> I would like to build an array of dictionaries. Most of the
> dictionary example on the net are for single dictionary.
>
> dict = {'a':'a','b':'b','c':'c'}
> dict2 = {'a':'a','b':'b','c':'c'}
> dict3 = {'a':'a','b':'b','c':'c'}
>
> arr = (dict,dict2,dict3)
>
> What is the syntax to access the value of dict3->'a'?
This isn't a special case.
arr[2] to get the dictionary
arr[2]['a'] to get the value in the dictionary
'a' in arr[2] to find if there is such a key
arr[2].get('a') to get the value or None if the key isn't there
arr[2].get('a', 'd') to get a value even if the key isn't there
help(dict.get)
for key in arr[2]:
# to iterate over the keys
The exact same mechanisms are used no matter where you get the
dictionary from.
More information about the Python-list
mailing list