3) f. Sets

Sets are bit more tricky than the other containers you have looked at.

  • Sets’ elements are not ordered.
  • Sets are mutable.
  • Sets can only contain unique values.

You can create a set by using braces or by using the set function. Similarly to other data structures this function can be used to convert other iterables into sets.
We will cover conversion between data structures in more details

1
2
3
Output:
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}




Methods:

Take a look at each method and try to guess the output. See how many you can guess correctly.

add(e): Add element e to the set.

1
2
Output:
{'Daniel', 'John', 'Alice', 'Michael', 'Alex', 'Nicholas'}




update(iterable, …): Add elements from one or more iterables.

1
2
Output:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}




discard(e): Remove element e from the set.

1
2
3
Output:
{'Daniel', 'Alice', 'Michael', 'Alex', 'Nicholas'}
{'Daniel', 'Alice', 'Michael', 'Alex', 'Nicholas'}




remove(e): Remove e from the set. If e is not in the set, raises KeyError.

1
2
3
Output:
{'Daniel', 'Alice', 'Michael', 'Alex', 'Nicholas'}
KeyError...




pop(): Remove and return an arbitrary element of the set.

1
2
3
4
Output:
Daniel
Alex
{'John', 'Michael', 'Alice'}




clear(): Remove all elements from the set.

1
2
Output:
{}




Time for a quick recap. Here is an image showing the most commonly used methods. See if you can recognize the concepts being depicted.




One of the main benefits of using sets is being able to compare between different sets easily. For example, you can quickly find what’s common between two sets.
Here are a few methods that showcase this ability.

issuperset(iterable): Return whether the set contains iterable.

1
2
Output:
True




issubset(iterable): Return whether the set is contained in iterable.

1
2
Output:
True




isdisjoint(iterable): Return whether the contents of the set have nothing in common with iterable.

1
2
Output:
True




difference(iterable, …): Return a new set with elements from the set that are not in provided one or more iterables.

1
2
Output:
{2}




intersection(iterable, …): Return a new set with elements from the set that are common with the elements of the provided one or more iterables.

1
2
Output:
{1, 3, 5, 7}




symmetric_difference(iterable): Return a new set with only elements either from the set or iterable but not both.

1
2
Output:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}




union(iterables, …): Return a new set with elements from the set and provided one or more iterables.

1
2
Output:
{1, 2, 3, 5, 7, 9}




difference_update(iterable, …): Update the set while removing elements that are in the provided one or more iterables.

1
2
Output:
{2}




intersection_update(iterable, …): Update the set while preserving only elements that are in both the set and the provided one or more iterables.

1
2
Output:
{1, 3, 5, 7}




symmetric_difference_update(iterable): Update the set while preserving only elements that are either in the set or in iterable but not both.

1
2
Output:
{2, 9}




copy(): Return a copy of the set.

1
2
Output:
{2, 4, 6, 8, 10}




Here is a summary of the important methods for sets: