A set is an unordered collection which does not allow duplicate elements.
a = [1,2,3,3,4,'yes','no'] print a # [1, 2, 3, 3, 4, 'yes', 'no'] print set(a) # set([1, 2, 3, 4, 'yes', 'no'])
The number 3 appeared only once in the set. Searching for items in set is also easy:
a = [1,2,3,3,4,'yes','no'] print set(a) print 'yes' in a # true print 'n' in a # false print 2 in a # true
Set Arithmetic
a = set('python')
b = set('php')
print b # unique letters
# set(['p', 'h'])
print a - b # difference
# set(['y', 't', 'o', 'n'])
print a | b # union
# set(['p', 't', 'y', 'h', 'o', 'n'])
print a & b # intersection
# set(['p', 'h'])
print a ^ b # symetric difference
# set(['y', 't', 'o', 'n'])