Set type
Contents
19. Set type#
Author: Tue Nguyen
19.1. Outline#
What is a set?
Create a set
Iterate through a set
Copy a set
Operations on sets
Frozen sets
19.2. What is a set?#
A set is an mutable collection of unique immutable elements
The set itself is mutable
But its elements must be immutable and unique
Elements of a set are not ordered
We cannot use indexing or slicing as with lists
There’s no way to access an element of a set
We can only check if a value is in a set or not
19.3. Create a set#
# We create a set using {}
s = {1, 2, 3}
print(s)
print(type(s))
{1, 2, 3}
<class 'set'>
# However, we cannot create an empty set using {}
# Because by default {} means an empty dict
# To create an empty set we use set()
s = set()
s
set()
19.4. Conversion to sets#
We use set()
to convert an object to a set
# List to set
set([1, 2, 3])
{1, 2, 3}
# Tuple to set
set((1, 2, 3))
{1, 2, 3}
# Dict to set (keep the keys)
d = {"k1": "v1", "k2": "v2"}
set(d)
{'k1', 'k2'}
# To keep the values of the dict
set(d.values())
{'v1', 'v2'}
# String to sets (auto remove duplicates)
set("this is a string")
{' ', 'a', 'g', 'h', 'i', 'n', 'r', 's', 't'}
19.5. Iterate through set#
Same as lists and tuples
19.6. Mutablity#
A set is mutable
We can add, update, or delete elements
19.7. Copy a set#
Same as lists (there will be both shallow and deep copy)
However, note that elements of set are immubtable so there will be less confusion
19.8. Operations on sets#
As a mutable type like lists, we also have two types of operations on sets
Regular operations
Inplace operations
19.8.1. Regular operations#
# Init 2 sets
s1 = {1, 2, 3, 4, 5}
s2 = {0, 1, 2, 3}
# Count number of elements
len(s1)
5
# Check membership
2 in s1
True
# Set union
s1 | s2
{0, 1, 2, 3, 4, 5}
# Set intersection
s1 & s2
{1, 2, 3}
# Set difference
s1 - s2
{4, 5}
# Symmetric difference
s1 ^ s2
{0, 4, 5}
# Check subset with < or <=
print(s1 <= s2)
print({1, 2} <= s2)
False
True
Remarks
There are methods for set operations such as
s1.union(s2)
,s1.intersection(s2)
, ors1.issubset()
However, using operators like
|
,&
, or<=
is shorter and more intuitive
19.8.2. Inplace operations#
# Init
s = {1, 2, 3, 4, 5}
s
{1, 2, 3, 4, 5}
# Add an element (if not yet existed)
s.add(10)
s
{1, 2, 3, 4, 5, 10}
# Remove an element
# (will raise an error if the value is not in s)
s.remove(2)
s
{1, 3, 4, 5, 10}
# We can use .discard for safer removal
# If value is in s, then remove
# If not, then do nothing (no error)
s.discard(100)
s
{1, 3, 4, 5, 10}
# Update a set from another set
s.update({1, 2, 88, 99})
s
{1, 2, 3, 4, 5, 10, 88, 99}
# Pop a random element from a set
# (because sets are not ordered)
v = s.pop()
print(v)
print(s)
1
{2, 3, 4, 5, 99, 10, 88}
19.9. Frozen sets#
A frozen set is an immutable set
Think of set and frozen set like lists and tuple
Frozen sets have the same regular operations as sets
They don’t have inplace operations because they are immutable
fs = frozenset([1, 2, 3])
print(fs)
print(type(fs))
frozenset({1, 2, 3})
<class 'frozenset'>
19.10. Summary#
What is a set?
A mutable collection of unique immutable elements
Elements of a set are not ordered
Create a set
Using
{}
orset()
Iterate through a set
Same as lists
Copy a set
Same as lists
Operations on sets
Regular operations:
len
,in
,|
,&
,-
,^
,<=
,<
Inplace operations:
.add()
,.remove()
,.discard()
,.update()
,.pop()
Frozen sets
Immutable versions of sets
Create a frozen set using
frozenset()
19.11. Practice#
To be updated