2
dict = {"a": 1, "b": 2, "c": 3, "d": 4}
for key, val in dict.items():
    if key == "b":
        dict["e"] = 5
for key, val in dict.items():
    print (key, val)

above code when executed in python3 it shows following error

RuntimeError: dictionary changed size during iteration
    for key, val in dict.items():
Line 4 in solve (Solution.py)
    ret = Solution().solve(param_1, param_2)
Line 33 in _driver (Solution.py)
    _driver()
Line 44 in <module> (Solution.py)

Where as the above code runs without error in python(version earlier that 3) and with following expected output.

('a', 1)
('c', 3)
('b', 2)
('e', 5)
('d', 4)

Is it due to this? As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

shubhcodegate
  • 123
  • 1
  • 6
  • 1
    Well, it must raise an error, as `dp` is not defined. – j1-lee Jul 23 '22 at 04:28
  • No... that isn't the reason... Altering the size of the dictionary has thrown an error since 3.0 from what I can remember I don't know what the real reason is – Alexander Jul 23 '22 at 04:32
  • 1
    @alexpdev python2 raised an exception when changing the size of a dict too. The difference is that `dict.keys()` and `dict.items()` returned lists, so you were not iterating over the dict when iterating over the keys/items. If you want to do something similar in python3, make a list of items: `items = list(dict.items())` then iterated over `items`. – Mark Jul 23 '22 at 04:35
  • @mark Why are you telling me? I didn't ask the question.... Sounds like you have enough knowledge to answer the OP's question – Alexander Jul 23 '22 at 04:37
  • @alexpdev I'm telling you because you left a comment that was completely wrong. – Mark Jul 23 '22 at 04:38
  • @mark How is my comment wrong? I accurately stated that changing the size of the dictionary has been in effect since 3.0.... and I said I don't know about the rest... Which part of the is wrong? – Alexander Jul 23 '22 at 04:39
  • It's wrong because changing the size of a dict you were iterating over was an error in python2 as well. `for key in somedict: del somedict[k]` is an error in python2. It was not introduced in version 3. See error [here](https://paiza.io/projects/snDtgUHoU1Od4qSAv2H75A?language=python) – Mark Jul 23 '22 at 04:41
  • what is dp? you cannot define . If you define dp or you can use dict instead of DP it can shows you an error. – Mehmaam Jul 23 '22 at 04:45
  • 1
    To say it was an error **since** 3.0 implies it was not an error before 3.0. If you didn't mean to suggest this was a different behavior since 3.0, your first comment makes no sense in the context of a question asking about behavior differences between the two versions. – Mark Jul 23 '22 at 04:47
  • I didn't answer because it's a dupe @alexpdev — just taking a while to find [because ....](https://xkcd.com/386/). – Mark Jul 23 '22 at 04:52
  • @j1-lee Sorry for the spelling mistake in "dp["e"] = 5". I meant "dict["e"] = 5". – shubhcodegate Aug 03 '22 at 10:25

0 Answers0