Can an empty key be used to insert a value into a dictionary?



Is it possible to accidentally insert a key into a dictionary which doesn’t have any meaning?


Just because a key doesn’t have a useful meaning, it can still be inserted into a dictionary as long as Python is able to compute a hash value for it. The following code example shows three keys which Python will accept without reporting an error. These keys are unlikely to be useful but are accepted by Python without complaint and could result in unexpected behavior.

mydict = {}

mykey = None
mydict[mykey] = 100

mykey = ''
mydict[mykey] = 200

mykey = False
mydict[mykey] = 300

# {None: 100, '': 200, False: 300}