[Tutor] Equivalent of Set in PtO

Alan Gauld alan.gauld at btinternet.com
Tue Apr 26 09:46:05 CEST 2011


"Becky Mcquilling" <ladymcse2000 at gmail.com> wrote

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

Can be abbreviated to:

def countDups(duplicateList):
    return [(item, duplicateList.count(item)) for item in 
set(duplicateList)]

> was using python version 2.7.  I want to do the same thing in Python 
> 3.1,
> but I'm not sure what has replaced Set in the newer version, can 
> someone
> give me an idea here?

set()

It has become a native type in v3.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list