3

It seems sets are deprecated in Python 2.7 and wondering what is the alternative for unordered unique collection? Thanks.

from sets import Set

a = Set()
a.add("1")
a.add("2")
a.add("3")

if "1" in a:
    print "1"
if "Hello" in a:
    print "Hello"

regards, Lin

0

1 Answer 1

10

Sets are still available in python 2.7 and are built in.

Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a_set = set([1, 2, 3])
>>> a_set
set([1, 2, 3])
>>> b_set = {1, 2, 3}
>>> b_set
set([1, 2, 3])

Docs: https://docs.python.org/2/library/sets.html And see the Notice at the top of the page:

Deprecated since version 2.6: The built-in set/frozenset types replace this module.

1
  • 1
    Thanks tknickman, mark your reply as answer. Works for me. :)
    – Lin Ma
    May 1, 2016 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.