0

I have a dict in the form

{'Friday': {'US Eastern (EST)': 'Feb-19', '1:00: AM': '100 reasons why God hates porn. 1/100: Love of God (Bible) /Jay', '7:00: PM': 'Do Not Be Conformed To This World The Whys and the Whats Bible Study w/Esc', '8:00: PM': 'Game Night w/ Sam Stardew Valley'}, 'Monday': {'US Eastern (EST)': 'Feb-15', '7:30: PM': 'Prayer Meeting', '8:00: PM': 'Board Game Night w/Esc Tabletop Simulator'}, 'Saturday': {'US Eastern (EST)': 'Feb-20', '8:00: AM': 'Bible study on Hope w/Joel', '8:30: PM': 'Ladies Hangout'}, 'Tuesday': {'US Eastern (EST)': 'Feb-16', '12:30: AM': 'Starting/Ending the day (part with 3)- What Psalm 16 can we learn about prayer from David? (Combinated) /Jay', '8:00: PM': 'Ask Anything w/ Pace RE: Ravi Zacharias Recent Findings', '9:00: PM': 'Game Night w/ Sir Labalot Rocket League + Among Us'}, 'Wednesday': {'US Eastern (EST)': 'Feb-17', '9:00: PM': 'Bible Before Bed w/ Sir Labalot (~45 Mins)'}}

Basically, all the days together with their times and events. I'm trying to replace the times with DateTime strings. (2021-02-16 00:30:00+00:00) So far, this is the code I've written but I'm getting a run time error (RuntimeError: dictionary changed size during iteration) and thus would like some help optimizing the code and seeing exactly what I'm doing wrong to cause the error.

from t import dictionary
from workingdicts import times

new = dictionary.copy()
x = times.splitlines()

#times has the DateTime strings and dictionary is the dict

for line in times.splitlines():
    for key in dictionary.copy():
        for day in new[key]:
            if 'EST' in day:
                continue
            else:
                new[key][line] = new[key].pop(day)
  
Maybe
  • 83
  • 1
  • 1
  • 8
  • @mkrieger1 No. I used that resource before posting here. That's why I made a copy of the dict before iterating. – Maybe Apr 09 '21 at 17:47
  • But you didn't make a copy of the dictionary `new[key]`, the one which you are removing items from, thereby changing its size during iteration. – mkrieger1 Apr 09 '21 at 18:05
  • @mkrieger1 Can you explain more? Maybe with an example. Because there are two copies in my code. – Maybe Apr 09 '21 at 18:09
  • You copied `dictionary`, but that's not the dictionary you are removing items from. You need to copy `new[key]`. Literally, replace `for day in new[key]` by `for day in new[key].copy()`. – mkrieger1 Apr 09 '21 at 18:40
  • @mkrieger1 thanks. I figured it out before you commented though. – Maybe Apr 09 '21 at 20:07

0 Answers0