Keith Dart wrote: > Aye... > > the dict.keys() line creates a temporary list, and then the 'in' does a > linear search of the list. Better would be: > > try: > dict[a].append(b) > except KeyError: > dict[a] = [b] > > since you expect the key to be there most of the time, this method is > most efficient. You optomistically get the dictionary entry, and on the > exceptional case where it doesn't yet exist you add it.
I wonder if dct.setdefault(a,[]).append(b) wouldn't be even faster. It saves setting up the try/except frame handling in python (I assume the C implementation of dicts achieves similar results with much less overhead). Cheers, f ps. I changed dict->dct because it's a generally Bad Idea (TM) to name local variables as builtin types. This, for the benefit of the OP (I know you were just following his code conventions). -- http://mail.python.org/mailman/listinfo/python-list