Consider the code
a = 2
b = 3
mylist = {'a' : a, 'b' : b, 'product' : a * b}
This produces a dictionary of three fields, of which the third is calculated using the values of the first and second. I am looking for a more compact definition of mylist
. I have tried (1)
mylist = {'a' : 2, 'b' : 3, 'product' : a * b}
which gives the error
NameError: name 'a' is not defined
and (2)
mylist = {'a' : 2, 'b' : 3, 'product' : mylist['a'] * mylist['b']}
which gives the error
NameError: name 'mylist' is not defined
I would like to find a shorter command of the form (1) because you do not need to need to mention the name of the dictionary. Maybe there exists something like currentdictionary['a']
?