Python 2.7 dictionary question

I don’t use dictionaries all the time, so I’m not a master. I need to create a rather long one this time, with over 100 entries. However, using the test script below, the first entry does not seem to get into the dictionary… Anyone know why?

#!/usr/bin/env python
# -*- coding: utf-8 -*-

my_dict['1111']='This is some text'
my_dict['1112']='Some other text'
my_dict['1113']='Still some more text'

print '1111' in my_dict
print '1112' in my_dict
print '1113' in my_dict

It also fails if I use my_dict=dict() instead.

And, if I remove the encoding utf-8 line at the top, I get a funny error…

Non-ASCII character '\xef' in file C:\Users... on line 4, but no encoding declared

So there must be some kind of incorrect assumption on my part here when I create the empty dictionary? This is Py 2, it has to run in V7 and earlier.

Edit - OK, it seems that the first entry had an “invisible character” in it. If it’s invisible how do you find it? The data for this dictionary was copied from somewhere else and despite passing through Notepad to remove any formatting, the character seems to have come with it. So just completely erasing the ‘1111’ and re-typing it now works.

The problem is, that “1111” you are setting is not the “1111” you are querying.

Check here:

I think your dict understanding is correct, but that the 1111 string appears to be corrupt somehow. I tried typing it in again (left) and that appears to work as expected: (4.4 KB)

Edit: @dn.aur was faster :running_man: