Business Benefits
Store unique values that can be used for developing efficient code.
Create sets using the syntax test_set = {item1, item2, item3}
to store numbers or sets of words.
Sets are encapsulated within curly brackets { }
. For example:
set1 = {3,22,1,56}
set2 = {‘hello’, ‘Sara’, 11, ‘Matt’, 23.89}
```Sets can only contain unique values. If you try creating a set with duplicate values, Python will automatically remove such values. For example, typing `set5` at the command line for the following set will return `{1, 2, "one", 3}`:
set5= {1,2,"one", 3, 1, "one"}
## Use the `len()` function to obtain the length and determine the number of items in a set.
For example, the `len()` function below would return 3, since there are three values in the set:
sample_set = {"one", "two", "three"}
>>> len(sample_set)
## Use the `max()`, `min()`, and `sum()` built-in functions to obtain the maximum, minimum, and sum of the values within the set.
For example, in the following sample set, `max(sample_set)` would return 3, `min(sample_set)` would return 1, and `sum(sample_set)` would return 6:
>>> sample_set = {1,2,3}
>>> max(sample_set)
>>> min(sample_set)
>>> sum(sample_set)
## Use the `add()` and `remove()` methods to add or remove values from a set.
For example, with the following sample set, `sample_set.add(5)` would result in `sample_set{1,2,3,4,5}`, and `sample_set.remove(4)` would result in `sample_set{1,2,3}`:
>>> sample_set = {1,2,3,4}
```If you try to remove an element that doesn’t exist in the set, the remove()
method will throw a KeyError exception.
You can also use discard()
method to remove the element from the set, but this method will ignore any errors. For instance, sample_set.discard(6)
used with the above sample set would not give a result or return an error.
Loop through sets using the for
method to iterate over the elements in a set and obtain the values in sequence or to obtain a specific value based on index.
For example:
sample_set = {1,2,3}
for i in sample_set:
… print(i)
…
1
2
3
Use issubset()
and issuperset()
to find out whether two sets contain any common values.
For example, set A
is a subset of B
, and both sets contain the common values 1
,2
, and 3
:
A = {1,2,3,4,5,6,7,8,9,10}
B = {1,2,3}
```You could use `issubset()` to find out whether all values of set `B` are values in Set `A`, and `issuperset()` to find out whether any of the elements in Set `A` are also in Set `B`:
>>>
>>> A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
>>> B = {1, 2, 3}
>>>
>>> A.issubset(B)
False
>>>
>>> B.issubset(A)
True
>>>
>>> B.issuperset(A)
False
>>>
>>> A.issuperset(B)
True
>>>
## Use relational operators like `=` , `>` , and ` to compare sets and find out whether a set is a subset or superset of another set.
For example:
A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {1, 2, 3}B < A
TrueA < B
FalseB > A
FalseA > B
True
```The ==
and !=
operators can also be used to test if two sets contain the same elements. For example:
>>>
>>> s1 = {1,3,2}
>>> s2 = {1,2,3}
>>>
>>> s1 == s2
True
>>> s1 != s2
False
>>>
>>> B >= {1,2,3}
True
>>>
>>> A >>
Use the ?
union method to combine two sets.
Symbolically, we would write a union between A and B as A ?
B. For example:
A = {10, 20, 30, 40}
B = {1000, 2000}
A ? B => {10, 20, 30, 40, 1000, 2000}
```You can also use the `union()` method or `|` operator to create unions and combine sets. For example:
>>>
>>> n1 = {2, 3, 4, 10}
>>> n2 = {10, 2, 100, 2000}
>>>
>>> n3 = n1.union(n2)
>>> n3
{2, 3, 4, 100, 10, 2000}
>>>
>>> n4 = n1 | n2
>>> n4
{2, 3, 4, 100, 10, 2000}
>>>
## Separate list values with a comma and enclose them in parentheses to create tuples with values that don't change.
In the example below, `t` is a tuple created with `alpha`, `delta`, and `omega` as its values.
t = (“alpha”, “delta”, “omega”)
t
(‘alpha’, ‘delta’, ‘omega’)type(t)