ViaTech
ViaTech

Reputation: 2843

Access Key of Dictionary inside self

Is it possible to access the key of a dictionary inside of the same dictionary?

For example, I know this doesn't work, but I want to do something like this:

d = {
    'a': 1,
    'b': 2,
    'c': d['a'] + d['b'] # or actually more like: self.a + self.b
}

print(d['c']) # 3

Upvotes: 1

Views: 65

Answers (1)

GZ0
GZ0

Reputation: 4273

You have to add the derived (key, value) pairs after contructing the dict.

d = {
    'a': 1,
    'b': 2
}
d['c'] = d['a'] + d['b']

Upvotes: 2

Related Questions