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...

1 Answer 1

6

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
5
  • I'd prefer months["January"]["current_date"] += 60 Nov 3, 2018 at 20:54
  • 1
    yes but using my way he also sees how to get the value
    – Tobi696
    Nov 3, 2018 at 20:56
  • 1
    right, but in-place add is more efficient so it doesn't look up 4 keys in the dicts, only 2. Nov 3, 2018 at 20:57
  • @T.Wimma Wow, I wasn't expecting that it would be so easy. Thank you! You just saved 4 hours of work. <3 Nov 3, 2018 at 20:58
  • hey it was a simple one, but answer is very complete so well done Nov 3, 2018 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.