This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: some unicode keys not found using in dictionary.keys()
Type: behavior Stage: resolved
Components: Interpreter Core, Unicode Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: axw, ezio.melotti, r.david.murray
Priority: normal Keywords:

Created on 2012-08-07 22:10 by axw, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg167645 - (view) Author: (axw) Date: 2012-08-07 22:10
This simple snippet demonstrates the misbehaviour. Confirmed on two amd64 machines, python 2.7.3. 

###################

import copy

uzlist = [u'abc', u'def', u'ghj', u'klm', u'zxc']
utzdict = {u'abc':1, u'def':2, u'ghj':3, u'klm':4, u'zxc':5}

utemplist = copy.copy(uzlist)
for m in utemplist:
    if m in utzdict.keys(): utemplist.remove(m)

# utemplist should be empty now - it is not!
>>> utemplist
[u'def', u'klm']

utemplist = copy.copy(uzlist)
for i in range(len(uzlist)):
   try: 
     if utzdict[ uzlist[i] ]: utemplist.remove( uzlist[i] )
   except KeyError: pass

# utemplist should be empty now. This time it is:
>>> utemplist
[]
msg167646 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-08-07 22:15
In the first example you are shrinking the list you are iterating over, so not all of the items in the list are going to be tested.  Try doing for m in uzlist instead.
msg167647 - (view) Author: (axw) Date: 2012-08-07 22:24
Thanks, I did not realize that. The behaviour is obviously correct.
History
Date User Action Args
2022-04-11 14:57:33adminsetgithub: 59784
2012-08-07 22:29:55r.david.murraysetresolution: not a bug
2012-08-07 22:24:21axwsetresolution: not a bug -> (no value)
messages: + msg167647
2012-08-07 22:15:58r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg167646

resolution: not a bug
stage: resolved
2012-08-07 22:10:45axwcreate