0

I have the following setup:

co_occurrences = defaultdict(lambda: defaultdict(int))
# Populate the dictionary...

for word, occurrence_vector in co_occurrences:
    if word == "__length": continue

    for file_name, occurrence_count in occurrence_vector:
        co_occurrences[word][file_name] = occurrence_count / co_occurrences["__length"][file_name] 

Is this line:

co_occurrences[word][file_name] = occurrence_count / co_occurrences["__length"][file_name]

dangerous? By dangerous, I mean that I want to iterate over each key once and only once, so any code that modfies this behavior is dangerous. I feel like it might be since I am modifying the data structure I am iterating over.

Max
  • 15,157
  • 17
  • 82
  • 127
  • 1
    You can work around it by using `for word in co_occurrences.keys(): occurrence_vector=co_occurrences[word]` which makes a copy of the list of keys and iterates over that instead. – Perkins Jun 13 '13 at 06:59
  • 1
    Also having a magic entry `"__length"` in your dict is really really weird. Just make a `file_length` dict that maps file names onto their lengths. – U2EF1 Jun 13 '13 at 07:02
  • Related : http://stackoverflow.com/a/2315529/846892 – Ashwini Chaudhary Jun 13 '13 at 07:04

2 Answers2

3

It generally is fine, as was mentioned, the only issues come if the size of the dictionary changes. If that happens, it will throw an Exception and stop executing, so if it executes without a RuntimeError, then whatever you are doing is fine.

Perkins
  • 2,409
  • 25
  • 23
2

It is only dangerous if you are changing the structure of the data you are iterating ie. adding / removing keys, otherwise it is completely fine to edit existing keys.

jamylak
  • 128,818
  • 30
  • 231
  • 230