I am trying to do something like this:
_dict = {"foo" : 1234,
"bar" : _dict["foo"] + 1}
Cant seem to get the syntax correct, is there a way to acomplish this without using multiple dictionaries, or defining them elsewhere?
You cannot access _dict
while defining it. Python first evaluates the {...}
dict literal before assigning it to _dict
. In other words, while evaluating the {...}
dict literal statement, _dict
is not yet defined and thus cannot be accessed.
Do this instead:
_dict = {"foo" : 1234}
_dict["bar"] = _dict["foo"] + 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With