[Tutor] Using a dict value in the same dict

Noah Hall enalicho at gmail.com
Tue Jul 5 18:52:13 CEST 2011


2011/7/5 Válas Péter <sulinet at postafiok.hu>:
> Hi,
>
> I have a dictionary with the keys 'a' and 'b'. It is not in a class. (I know
> that everything is in a class, but not explicitly.)
> May I use the value of 'a' when defining the value of 'b'? If so, what is
> the syntax?

Yes. The syntax is the same as anything involving a dict -

>>> a_dict = dict()
>>> a_dict['a'] = 2
>>> a_dict['b'] = a_dict['a'] + 1
>>> a_dict
{'a': 2, 'b': 3}


More information about the Tutor mailing list