In <[EMAIL PROTECTED]>, Bob Kline wrote: > I have a suggestion for speeding up the performance of code like this: > > fields = cgi.FieldStorage() > if fields: ... > > which, as it turns out, invokes FieldStorage.__len__(), which in turn > calls FieldStorage.keys(), which builds a list of keys by hand, taking > several minutes for large forms. This implementation of keys() reduces > the amount of time taken by several orders of magnitude: > > def keys(self): > return {}.fromkeys([i.name for i in self.list]).keys()
This does not maintain the order of `self.list`. Don't know if there's code relying on this. In a recent Python version one could use `set()` and a generator expression:: return list(set(item.name for item in self.list)) But maybe it's even faster to change `FieldStorage.__len__()` to return the length of `self.list` directly without the need to create an intermediate list that's thrown away immediately. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list