[Tutor] Equivalent of Set in PtO

Steven D'Aprano steve at pearwood.info
Tue Apr 26 09:50:20 CEST 2011


Becky Mcquilling wrote:
> I have a code snippet that I have used to count the duplicates in a list as
> such:
> 
> from sets import Set

Since Python 2.4, you no longer need to import module "sets" (note 
plural) to get Set (note capital letter). You can just use the built-in 
name "set" (note lower-case letter).


> def countDups(duplicateList):
>   uniqueSet = Set(item for item in duplicateList)
>   return[(item, duplicateList.count(item)) for item in uniqueSet]

This becomes:

uniqueSet = set(item for item in duplicateList)


Another advantage is that the built-in set is much faster than sets.Set.




-- 
Steven



More information about the Tutor mailing list