Unicode and dictionaries

Carl Banks pavlovevidence at gmail.com
Sun Jan 17 03:18:46 CET 2010


On Jan 16, 3:56 pm, Ben Finney <ben+pyt... at benfinney.id.au> wrote:
> gizli <mehm... at gmail.com> writes:
> > >>> test_dict = {u'öğe':1}
> > >>> u'öğe' in test_dict.keys()
> > True
> > >>> 'öğe' in test_dict.keys()
> > True
>
> I would call this a bug. The two objects are different, so the latter
> expression should return ‘False’.

Except the two objects are not different if default encoding is utf-8.

(Whether it's a good idea to change the default encoding is another
question, but Python is clearly documented as behaving this way.  When
comparing a byte string and a Unicode string, the byte string will be
decoded according to the default encoding.)


> FYI, ‘foo in bar.keys()’ is easier to spell as ‘foo in bar’.

I believe the OP's point was to show that dicts behave differently
than lists here ("in" works for lists, doesn't work for dicts).


Carl Banks



More information about the Python-list mailing list