From: "Jeremy Hylton" email@example.com
RH> Second wild idea of the day: The dict constructor currently RH> accepts sequences where each element has length 2, interpreted RH> as a key-value pair.
RH> Let's have it also accept sequences with elements of length 1, RH> interpreted as a key:None pair.
That seems a little too magical to me.
RH> Raymond Hettinger 'regnitteh dnomyar'[::-1] Then again it seems like you like magic!
While I'm a fan of performance magic, a la the Magic Castle, the root of this suggestion is more mundane. There are too many pieces of code that test membership with 'if elem in container' where the container is not a dictionary. This results in O(n) performance rather than O(1). To fix it, I found myself writing the same code over and over again:
def _toset(container): return dict([(elem, True) for elem in container])
This repeated dictionary construction exercise occurs in so many guises that it would be worthwhile to provide a fast, less magical looking approach. Being able to construct dictionaries with default values isn't exactly the most exotic idea ever proposed.
IMO, it's clearer, faster, commonly needed, and easy to implement.