dictionariesthe in list scans of the original set can be replaced with direct dictionary fetches in this scheme in traditional termsmoving sets to dictionaries replaces slow linear searches with fast hashtable fetches computer scientist would explain this by saying that the repeated nested scanning of the list-based intersection is an exponential algorithm in terms of its complexitybut dictionaries can be linear the module in example - implements this idea its class is subclass of the original setand it redefines the methods that deal with the internal representation but inherits others the inherited and methods trigger the new intersect and union methods hereand the inherited len method works on dictionaries as is as long as set clients are not dependent on the order of items in setmost can switch to this version directly by just changing the name of the module from which set is importedthe class name is the same example - pp \dstruct\basic\fastset py "optimize with linear-time scans using dictionariesimport set fastset set extends set set class set(set set)def __init__(selfvalue [])self data {self concat(valuedef intersect(selfother)res {for in otherif in self datares[xnone return set(res keys()manages local dictionary hashinglinear search times othera sequence or set use hash-table lookup def union(selfother)res {for in otherres[xnone for in self data keys()res[xnone return set(res keys() new dictionary-based set othera sequence or set scan each set just once '&and '|come back here so they make new fastset' def concat(selfvalue)for in valueself data[xnone inherit andorlen def __getitem__(selfix)return list(self data keys())[ix xlist(def __repr__(self)return 'list(self data keys()ditto implementing sets