In particular, #7 is something that I didn't even know existed, and I've been hacking around for 2+ years.
Instead of:
mdict={'gordon':10,'tim':20}
>>> print mdict.get('gordon',0)
10
>>> print mdict.get('tim',0)
20
>>> print mdict.get('george',0)
0
I've always done the much more verbose: class defaultdict(dict):
def __init__(self, default=None):
dict.__init__(self)
self.default = default
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError:
return self.default
mdict=defaultdict(0)
mdict['gordon']=10
mdict['tim']=20
print mdict['gordon']
10
print mdict['tim']
20
print mdict['george']
0
I'll be sure to make great use of the dictionary get method - I'm embarrassed to admit how many thousands of times I could have used that, and didn't know it existed.