[Tutor] set(...)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Mar 17 22:26:13 CET 2005



> I use:
> --------------------------------------------------------------------------------
> Python 2.3.4 (#2, Aug 19 2004, 15:49:40) [GCC 3.4.1 (Mandrakelinux (Alpha
> 3.4.1-3mdk)] on linux2 ... IDLE 1.0.3
> --------------------------------------------------------------------------------
> I was trying to write:
> --------------------------------------------------------------------------------
> >>> basket=['apple','orange','apple','pear','orange','banana']
> >>> fruits=set(basket)
> --------------------------------------------------------------------------------
> and I get:
> --------------------------------------------------------------------------------
> Traceback (most recent call last): File "<pyshell#322>", line 1, in
> -toplevel- fruits=set(basket) NameError: name 'set' is not defined
> >>>
> --------------------------------------------------------------------------------
> What's wrong? How to use sets?


Hi Logan,

Ah!  I think you looking for the set() builtin, which was added in Python
2.4.

But you're still using Python 2.3.4, when sets weren't "built in" yet.
They're still accessible, but you have to do a module import in 2.3. You
can make your example work by doing the following at the beginning:

###
from sets import Set as set
###


Best of wishes to you!



More information about the Tutor mailing list