res self data[:for in otherif not in resres append(xreturn set(resdef concat(selfvalue)for in valueif not in self dataself data append(xmake copy of my list valuea liststringset filters out duplicates def __len__(self)return len(self datadef __getitem__(selfkey)return self data[keydef __and__(selfother)return self intersect(otherdef __or__(selfother)return self union(otherdef __repr__(self)return 'the set class is used like the stack class we saw earlier in this we make instances and apply sequence operators plus unique set operations to them intersection and union can be called as methodsor by using the and operators normally used for built-in integer objects because we can string operators in expressions now ( )there is no obvious need to support multiple operands in intersect/union methods here (though this model' need to create temporary objects within expressions might eventually come to bear on performanceas with all rightly packaged objectswe can either use the set class within program or test it interactively as followsfrom set import set users set(['bob''emily''howard''peeper']users set(['jerry''howard''carol']users set(['emily''carol']users users users users users users users (users users users users data ['bob''emily''howard''peeper'optimizationmoving sets to dictionaries once you start using the set classthe first problem you might encounter is its performanceits nested for loops and in scans become exponentially slow that slowness may or may not be significant in your applicationsbut library classes should generally be coded as efficiently as possible one way to optimize set performance is by changing the implementation to use dictionaries rather than lists for storing sets internally--items may be stored as the keys data structures