# Python Programming/Sets

| Previous: Dictionaries | Index | Next: Operators |
Starting with version 2.3, Python comes with an implementation of the mathematical set. Initially this implementation had to be imported from the standard module set, but with Python 2.6 the types set and frozenset became built-in types. A set is an unordered collection of objects, unlike sequence objects such as lists and tuples, in which each element is indexed. Sets cannot have duplicate members - a given object appears in a set 0 or 1 times. All members of a set have to be hashable, just like dictionary keys. Integers, floating point numbers, tuples, and strings are hashable; dictionaries, lists, and other sets (except frozensets) are not.

### Overview

Sets in Python at a glance:

### Constructing Sets

One way to construct sets is by passing any sequential object to the "set" constructor.
We can also add elements to sets one by one, using the "add" function.
Note that since a set does not contain duplicate elements, if we add one of the members of s to s again, the add function will have no effect. This same behavior occurs in the "update" function, which adds a group of elements to a set.
Note that you can give any type of sequential structure, or even another set, to the update function, regardless of what structure was used to initialize the set.
The set function also provides a copy constructor. However, remember that the copy constructor will copy the set, but not the individual elements.

### Membership Testing

We can check if an object is in the set using the same "in" operator as with sequential data types.
We can also test the membership of entire sets. Given two sets $S_{1}$ and $S_{2}$, we check if $S_{1}$ is a subset or a superset of $S_{2}$.
Note that "issubset" and "issuperset" can also accept sequential data types as arguments
Note that the <= and >= operators also express the issubset and issuperset functions respectively.
Like lists, tuples, and string, we can use the "len" function to find the number of items in a set.

### Removing Items

There are three functions which remove individual items from a set, called pop, remove, and discard. The first, pop, simply removes an item from the set. Note that there is no defined behavior as to which element it chooses to remove.
We also have the "remove" function to remove a specified element.
However, removing a item which isn't in the set causes an error.
If you wish to avoid this error, use "discard." It has the same functionality as remove, but will simply do nothing if the element isn't in the set
We also have another operation for removing elements from a set, clear, which simply removes all elements from the set.

### Iteration Over Sets

We can also have a loop move over each of the items in a set. However, since sets are unordered, it is undefined which order the iteration will follow.

### Set Operations

Python allows us to perform all the standard mathematical set operations, using members of set. Note that each of these set operations has several forms. One of these forms, s1.function(s2) will return another set which is created by "function" applied to $S_{1}$ and $S_{2}$. The other form, s1.function_update(s2), will change $S_{1}$ to be the set created by "function" of $S_{1}$ and $S_{2}$. Finally, some functions have equivalent special operators. For example, s1 & s2 is equivalent to s1.intersection(s2)

#### Intersection

Any element which is in both $S_{1}$ and $S_{2}$ will appear in their intersection.

#### Union

The union is the merger of two sets. Any element in $S_{1}$ or $S_{2}$ will appear in their union.
Note that union's update function is simply "update" above.

#### Symmetric Difference

The symmetric difference of two sets is the set of elements which are in one of either set, but not in both (also called exclusive-or in logic).

#### Set Difference

Python can also find the set difference of $S_{1}$ and $S_{2}$, which is the elements that are in $S_{1}$ but not in $S_{2}$.

### Multiple sets

Starting with Python 2.6, "union", "intersection", and "difference" can work with multiple input. For example, using "set.intersection":

### frozenset

A frozenset is basically the same as a set, except that it is immutable - once it is created, its members cannot be changed. Since they are immutable, they are also hashable, which means that frozensets can be used as members in other sets and as dictionary keys. frozensets have the same functions as normal sets, except none of the functions that change the contents (update, remove, pop, etc.) are available.

### Exercises

* Create the set {'cat', 1, 2, 3}, call it s.
* Create the set {'c', 'a', 't', '1', '2', '3'}.
* Create the frozen set {'cat', 1, 2, 3}, call it fs.
* Create a set containing the frozenset fs, it should look like {frozenset({'cat', 2, 3, 1})}.

### Reference

* Python Tutorial, section "Data Structures", subsection "Sets" -- python.org
* Python Library Reference on Set Types -- python.org
* PEP 218 -- Adding a Built-In Set Object Type, python.org, a nice concise overview of the set type
| Previous: Dictionaries | Index | Next: Operators |