> You can check if the dictionary key exists prior to assigning to it:
>
> >>> if not D.has_key('c'):
> ...    D['c'] = {}
> >>> D['c']['a'] = 1


Hi Victor,

Another approach is to use the badly-named "setdefault()" method which is
a close analogue to Perl's "autovivification" feature:

######
>>> D = {}
>>> D.setdefault('c', {})['a'] = 1
>>> D
{'c': {'a': 1}}
######

Good luck!

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to