set
is mutable, iterable, but Indexing/Slicing doesn't work.set
should be hashable (similar to keys of dictionaries), that object must have a __hash__()
method in its class. All immutable objects are valid members of a set
. For example, int
, float
, str
& tuple
etc are all hashables. This behaviour allows sets to check if a particular object is unique from other members and also to perform operations like intersection()
, union()
.set
. Like in BFS/DFS algorithms for checking visited nodes.set
.## Create a empty set
my_set = set()
## Insert values inside '{}' brackets
my_set = {9, 1, 5, 2, 20}
# Notice: dictionary like parenthesis but without keys
# items order don't matter, so while printing order might differ
print(my_set) # {1, 2, 20, 5, 9}
set
.my_var = 4
my_set = {9, 1, 5, 2, 20}
## Adding items: If it is a repeated value, it will not be added again
my_set.add(my_var)
my_set.add(my_var)
print(my_set) # {1, 2, 20, 5, 4, 9}
## Removing items: Removes a item, raises 'KeyError' if not found
my_set.remove(my_var)
## Accessing members: Indexing is not supported
my_set[0] # TypeError: 'set' object is not subscriptable
# so use the 'in' operator to check if 'my_var' is inside 'my_set'
if my_var in my_set:
print("not printed")
set
.a = {54, 23, 67}
b = {34, 65, 55.6}
## Joining: Join two sets, '+' operator is not supported, use the update method
a.update(b)
print(a) # {65, 34, 67, 55.6, 54, 23}
## Iterating: Going over item by item from a set
for x in a:
print(x) # {65, 34, 67, 55.6, 54, 23}
set
comprehension.# Creating a set using comprehensions
my_set = {a for a in (10, 10, 20, 30, 60, 20, 40)}
# similar to previous comprehensions, there is also multiple comprehension
my_set = {(a, b) for a in range(2) for b in range(3)}
# Notice: (a,b) is a tuple inside a set
# above expression is similar to
my_set = set()
for a in range(2):
for b in range(3):
my_set.add((a, b))
print(my_set) # {(0, 1), (1, 2), (0, 0), (1, 1), (0, 2), (1, 0)}
# you can try practicing different combinations of comprehensions
set
.my_set1 = {3, 5, 7, 1, 8}
my_set2 = {1, 2, 3, 4, 5}
# Find intersection, similar to 'my_set1 & my_set2'
print(my_set1.intersection(my_set2)) # {1, 3, 5}
# Find union, similar to 'my_set1 | my_set2'
print(my_set1.union(my_set2)) # {1, 2, 3, 4, 5, 7, 8}
# Find difference between 'my_set1' and 'my_set2'
print(my_set1.difference(my_set2)) # {8,7}
# Checks if 'my_set2' is a subset of 'my_set1'
print(my_set1.issubset(my_set2)) # False
# Checks if 'my_set2' is a superset of 'my_set1'
print(my_set1.issuperset(my_set2)) # False
# Remove all members of set
my_set2.clear()
# Create a copy of a set, a shallow copy
my_copy = my_set1.copy()
# A shallow copy means copying only references of original
# items into a new sequence, so if a item isn't a literal constant
# like a list, any modification made inside that list will be
# reflected to that item of a new copy
my_list = [1, 2, 3, 4, 5]
## list to set
# here 'my_list' is mutable, so will raises 'TypeError'
my_set = {my_list} # TypeError: unhashable type: 'list'
# but 'set()' function unpacks the items from 'my_list'
my_set = set(my_list)
# also if 'my_list' contained a list inside it, 'TypeError' is raised
## tuple to set
my_set = set((1, 2, 3, 4, 5))
Time Complexity
in
operator) and removing are O(1).set
but the only difference is that they are immutable. So once a frozenset
is created the elements/members cannot be removed/added. Rest is pretty much similar to set
, they are a non repeating sequence of items, unordered, iterable and indexing/slicing is not supported. They can be created using any iterable object.frozenset
.## Create a frozenset, using a list
my_fset = frozenset([10, 65, 65, 2, 7, 94, 34, 42, 21])
# or any other iterable
my_fset = frozenset((10, 65, 65, 2, 7, 94, 34, 42, 21))
print(type(my_fset)) # <class 'frozenset'>
print(my_fset) # frozenset({65, 2, 34, 7, 10, 42, 21, 94})
## Immutable
my_fset.add(20) # AttributeError: 'frozenset' object has no attribute 'add'
my_fset.remove(20) # AttributeError: 'frozenset' object has \
# no attribute 'remove'
frozenset
.my_fset1 = frozenset({3, 5, 7, 1, 8})
my_fset2 = frozenset({1, 2, 3, 4, 5})
# Intersection
print(my_fset1.intersection(my_fset2)) # frozenset({1, 3, 5})
# Union
print(my_fset1.union(my_fset2)) # frozenset({1, 2, 3, 4, 5, 7, 8})
# Difference
print(my_fset1.difference(my_fset2)) # frozenset({8, 7})
# Create a copy of a set
my_copy = my_fset1.copy()
# Check if 'my_fset2' is a subset of 'my_fset1'
print(my_fset1.issubset(my_fset2)) # False
# Check if 'my_fset2' is a superset of 'my_fset1'
print(my_fset1.issuperset(my_fset2)) # False