Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I update values in a nested dictionary in python without getting error "dictionary changed size during iteration"?

Python beginner here. The project I’m working on uses a nested dictionary of different business locations and their hours by day of week. It’s formatted like this:

all_branch_hours = {
    'Branch 1': {
            'Sunday': '12:00-18:00',
            'Monday': '09:00-18:00',
            'Tuesday': '09:00-20:00',
            'Wednesday': '09:00-20:00',
            'Thursday': '09:00-20:00',
            'Friday': '12:00-18:00',
            'Saturday': '10:00-18:00'
    },
    'Branch 2': {
            'Sunday': '13:00-17:00',
            'Monday': '10:00-18:00',
            'Tuesday': '10:00-20:00',
            'Wednesday': '13:00-20:00',
            'Thursday': '10:00-18:00',
            'Friday': '13:00-18:00',
            'Saturday': '10:00-18:00'
    },
    'Branch 3': {
            'Sunday': '13:00-17:00',
            'Monday': '10:00-18:00',
            'Tuesday': '10:00-20:00',
            'Wednesday': '10:00-20:00',
            'Thursday': '10:00-20:00',
            'Friday': '13:00-18:00',
            'Saturday': '10:00-18:00'
    }
}

I want to generate a new dictionary where the hours are converted to lists of integers so that I can run a script later that checks whether or not a time is within open hours. For example the string "12:00-18:00" would become list [12,13,14,15,16,17].

Here’s what I tried:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import copy
datetime_branch_hours = copy.deepcopy(all_branch_hours)

for key, value in datetime_branch_hours.items():
        for k in value.values():
             openhour= int(k[0:2])
             closehour= int(k[6:8])
             open_hour_range = list(range(openhour, closehour))
             value[k] = open_hour_range`

I’ve tried this which I though should work, but I keep getting "RuntimeError: dictionary changed size during iteration". I know that editing a copy can avoid this error, but I don’t know how to do that with a nested dictionary. Any help would be greatly appreciated!

>Solution :

You are using wrong key for the inner dictionaries, causing addition of new keys while iterating. You need to iterate over the items of the inner dictionaries to get access to their true keys.

for key, value in all_branch_hours.items():
        for k, v in value.items():  # now k is truly key of inner dictionaries
            openhour= int(v[0:2])
            closehour= int(v[6:8])
            open_hour_range = list(range(openhour, closehour))
            value[k] = open_hour_range
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading