Sets
Sets
First: What is a 'set'?
In very short, a set
is a collection with no duplicates.
For example: We have a list of names: names = ['Alice', 'Bill', 'Jane', 'Jack', 'Bill']
. Bill
is in that list twice. When we call set(names)
, we will get back that list with Bill
there only once. ['Alice', 'Bill', 'Jane', 'Jack']
The Syntax
set(iterable)
frozenset(iterable)
- The iterable can be any iterable - list, string, etc.
**More about sets from the docs
A few more things about sets
-
There are 2 types of sets: the regular
set()
andfrozenset()
.-
set is mutable. So we can add or remove or update items in the set.
-
frozenset is immutable. And we cannot change anything about the set.
-
-
Sets are not ordered.
Once we are working with sets, we can do a few more cool things like:
These operations only work on set
and frozenset
datatypes.
operation # | description | syntax 1 | syntax 2 | results |
---|---|---|---|---|
* If operations mix a set
and frozenset
, then the result is an instance of the first. So the result of set1 | frozenset2
is a set
. The result of frozenset1 | set2
is a frozenset
.
A few examples:
All these examples use these lists:
co_workers =["Zach", "Dave", "Jack", "Emily", "Zach"]
friends = ["Mary", "Lisa", "Jack", "Mary"]
neighbors = ["Annie", "Bill", "Mary", "Annie"]
Notice that each of those lists has a duplicate. So to make sure we don't include any duplicates, we'll call set()
on each.
>>>set_coworkers = set(co_workers)
set(["Zach", "Dave", "Jack", "Emily"])
>>>set_friends = set(friends)
set(["Mary", "Lisa", "Jack"])
>>>set_neighbors = set(neighbors)
set(["Annie", "Bill", "Mary"])
intersection --> return all elements that all sets have in common
>>>set_neighbors.intersection(set_friends)
set(['Mary'])
>>>set_neighbors&set_friends
set(['Mary'])
union --> return all elements from all sets
>>>set_neighbors.union(set_friends).union(set_coworkers)
set(['Zach', 'Bill', 'Dave', 'Jack', 'Annie', 'Lisa', 'Mary', 'Emily'])
>>>set_neighbors|set_friends|set_coworkers
set(['Zach', 'Bill', 'Dave', 'Jack', 'Annie', 'Lisa', 'Mary', 'Emily'])
difference --> return all elements that are in set1 but not in set 2
>>>set_neighbors.difference(set_friends)
set(['Bill', 'Annie'])
>>>set_neighbors-set_friends
set(['Bill', 'Annie'])
>>>set_friends.difference(set_neighbors)
set(['Lisa', 'Jack'])
>>>set_friends-set_neighbors
set(['Lisa', 'Jack'])
symmetric_difference --> return all elements that in either set1 or set2, but not both.
>>> set_friends.symmetric_difference(set_neighbors)
set(['Lisa', 'Bill', 'Jack', 'Annie'])
>>> set_friends^set_neighbors
set(['Lisa', 'Bill', 'Jack', 'Annie'])
>>>set_friends.symmetric_difference(set_neighbors)
set(['Lisa', 'Bill', 'Jack', 'Annie'])
>>>set_neighbors^set_friends
set(['Lisa', 'Bill', 'Jack', 'Annie'])
Another set of operations for sets (only for set; not frozenset - as they are immutable)
operation # | description | syntax 1 | syntax 2 | results |
---|---|---|---|---|
** can have many .update(set3) |
||||
** can have many more |
||||