Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output of "Modifying a dictionary while iterating over it" is not correct for all Python versions #53

Closed
icemac opened this issue Dec 22, 2017 · 3 comments
Labels

Comments

@icemac
Copy link

icemac commented Dec 22, 2017

The result shown in Modifying a dictionary while iterating over it is correct for CPython 2.4 up to 3.5.

On Python 3.6 it prints

0
1
2
3
4

On PyPy 2.7 and PyPy 3 it prints

0
@prehensilecode
Copy link

For Python 3.6.4 on macOS 10.12:

Python 3.6.4 (default, Dec 20 2017, 18:41:55)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: x = {0: None}

In [2]: for i in x:
   ...:     del x[i]
   ...:     x[i+1] = None
   ...:     print(i)
   ...:
0
1
2
3
4

In [3]:

@satwikkansal
Copy link
Owner

Hey @icemac, thanks for reporting this. I'll investigate the reason for different behavior in the latest versions of Python.

@satwikkansal
Copy link
Owner

satwikkansal commented Jan 11, 2018

Okay, I dug up a little and found these differences in the implementation between Python 3.5 and 3.6 which explains the behavior.

In both the CPython versions, the hash table for dictionary starts with a size of 8 (Python 3.6.1 and Python 3.5.1). The resize occurs when the table size exceeds the USABLE_FRACTION which is different for both the Python versions ((2n/3 for Python 3.6.1 and (2n+1)/3 for Python 3.5.1.

Moreover, the way deleted keys are handled are probably different in both the Python version (I'm still trying to understand how they work), which leads to all the difference.

I think this is highly implementation specific, so I've added a note in the example regarding the Python versions for now, and I'll link this issue in the example description (for further reading of interested readers) once we figure out how deletions are handled in both the versions of python.

muscliary pushed a commit to muscliary/wtfpython that referenced this issue Sep 12, 2023
muscliary pushed a commit to muscliary/wtfpython that referenced this issue Sep 12, 2023
tothetop430 added a commit to tothetop430/wtfpython that referenced this issue Dec 9, 2023
tothetop430 added a commit to tothetop430/wtfpython that referenced this issue Dec 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants