SETS
A set is an unordered collection that does not contain duplicate elements. Use curly braces or the set() function to create sets. Set objects support set-theoretic operations such as union, intersection, and difference.
NOTE
set() is required in order to create an empty set because {} creates an empty dictionary.
The following code snippets illustrate how to work with a set.
Create a list of elements:
>>> l = ['a', 'b', 'a', 'c']
Create a set from the preceding list:
>>> s = set(l)
>>> s
set(['a', 'c', 'b'])
Test if an element is in the set:
>>> 'a' in s
True
>>> 'd' in s
False
>>>
Create a set from a string:
>>> n = set('abacad')
>>> n
set(['a', 'c', 'b', 'd'])
>>>
Subtract n from s:
>>> s - n
set([])
Subtract s from n:
>>> n - s
set([&apos...