Python Forum
RuntimeError: dictionary changed size during iteration
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RuntimeError: dictionary changed size during iteration
#2
I am not getting the error that you did, you didn't give a sample of word_sent so i made my own.

You should give a proper sample of word_sent so it can be tested in the same condition.
from collections import defaultdict

word_sent = [
    ["word", "word2", "word3", "word"],
    ["word5", "word1", "word1", "word"],
    ["word5", "word2", "word2", "word"],
]

freq = defaultdict(int)
for sentence in word_sent:  # word_sent is my list of lists that I mentioned above.
    for word in sentence:
        freq[word] += 1

print(freq)
Output:
defaultdict(<class 'int'>, {'word': 4, 'word2': 3, 'word3': 1, 'word5': 2, 'word1': 2})
Reply


Messages In This Thread
RE: RuntimeError: dictionary changed size during iteration - by Yoriz - Jun-01-2019, 11:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  help RuntimeError: no running event loop marpaslight 5 3,940 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  The behavior of tune model has changed Led_Zeppelin 5 4,588 Oct-21-2021, 06:52 PM
Last Post: jefsummers
  bleak library RuntimeError: This event loop is already running alice93 3 4,228 Sep-30-2021, 08:06 AM
Last Post: alice93
  how can a variable change if I haven't changed it? niminim 5 3,183 Apr-07-2021, 06:57 PM
Last Post: niminim
  RuntimeError: generator raised StopIteration quest 1 5,879 Mar-28-2021, 08:11 PM
Last Post: quest
  new help with dictionary and dataframe iteration AlphFinan 0 1,570 Oct-13-2020, 11:04 PM
Last Post: AlphFinan
  RuntimeError: This event loop is already running newbie2019 2 7,032 Sep-30-2020, 06:59 PM
Last Post: forest44
  RuntimeError: Optimal parameters not found: Number of calls to function has reached m bntayfur 0 6,216 Aug-05-2020, 04:41 PM
Last Post: bntayfur
  Dictionary iteration and creation a new dictionary from duplicates xrsxlnx 2 2,210 Mar-30-2020, 10:43 AM
Last Post: xrsxlnx
  size of set vs size of dict zweb 0 2,193 Oct-11-2019, 01:32 AM
Last Post: zweb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020