0

Have a dictionary which has a list of dictionaries which will be altered but getting the error:

RuntimeError: dictionary changed size during iteration

This was working with python2 but not with python3. How to make it work with python3?

multi_van_data = {'Van1': [{'ClusterStops': [], 'OptimalStop': 'GSV07858'},
                           {'ClusterStops': [], 'OptimalStop': 'GSV07932'},             
                           {'ClusterStops': [], 'OptimalStop': 'GSV07903'},
                           {'OptimalStop': 'GSV07892', 'ClusterStops': ['GSV07903']}]}

for k, v in multi_van_data.items():
    for val in v:
        val['ParentSequence'] = route.index(val['OptimalStop']) + 1
        val['ParentServiceTime'] = 1
        val['ParkstopTravelTime'] = 1
        val['ApproxCompletionTime'] = []
        cluster_stops = val['ClusterStops']
        print cluster_stops
        val.pop('ClusterStops')

Want to remove all ClusterStops

jww
  • 97,681
  • 90
  • 411
  • 885
SUP
  • 349
  • 1
  • 2
  • 11
  • @Jean-FrançoisFabre, seems like it worked. Thanks for helping out. – SUP Jan 03 '19 at 10:46
  • @Jean-FrançoisFabre Why did it fail before? The duplicate question shows the dictionary being modified, but OP here doesn't modify the dictionary. – Peter Wood Jan 03 '19 at 10:57
  • because `items()` returned a copy of the items in python 2. So no need to convert to `list`. So, yes, OP is modifying the dictionary right now when using python 3. – Jean-François Fabre Jan 03 '19 at 12:53
  • @Jean-FrançoisFabre which line of code first modifies the dictionary? – Peter Wood Jan 03 '19 at 17:54
  • 1
    none, actually. Code works fine, my bad. But print without parentheses suggests that this hasn't been run with python 3. And `route` isn't defined, which means that this isn't a [mcve]. Reopening but will recommend "cannot reproduce" (damn I shouldn't have reopened, OP commented that my suggestion fixed the issue!!!). Well that's a mess. – Jean-François Fabre Jan 03 '19 at 18:21
  • 1
    Possible duplicate of [How to avoid "RuntimeError: dictionary changed size during iteration" error?](https://stackoverflow.com/q/11941817/608639) – jww Jan 03 '19 at 23:46

0 Answers0