How to change the value of a key in a dictionary which is in a dictionary

2286 views python
-1

How can I change the value of the key "current_date". I've tried it with months[month].update({current_date: int(minute)}), but that only gave me a new dictionary and didn't change my old one. So how can I add time, for example 60 min?

Current Dictionary
months = {"January":{"current_date": 60}}

Update Dictionary
months = {"January":{"current_date": 120}}

I'm sorry if this question already got asked, but I simply wasn't able to find a solution...

answered question

1 Answer

10

I hope it is what you're looking for:

months = {"January":{"current_date": 60}} #initializing
months["January"]["current_date"] = 120 #updating

To add 60 mins:

months = {"January":{"current_date": 60}}
months["January"]["current_date"] = months["January"]["current_date"] + 60

Or more efficient:

months = {"January":{"current_date": 60}}
months["January"]["current_date"] += 60

posted this

Have an answer?

JD

Please login first before posting an answer.