Set & Frozen Set
Set & Frozen Set
Note: Set items are unchangeable, but you can remove or add items in set.
The set can be created by enclosing the comma-separated immutable items with the
curly braces {}
Unlike other collections in Python, there is no index attached to the elements of the
set, i.e., we cannot directly access any element of the set by the index.
However, we can print them all together, or we can get the list of elements by looping
through the set.
set1 = {10, 20, 30, 20}
print(set1)
print(len(set1))
Note: Sets are unordered, so you cannot be sure in which order the items will appear.
Set items are unordered, unchangeable, and do not allow duplicate values.
Unordered means that the items in a set do not have a defined order.
Set items can appear in a different order every time you use them, and cannot be
referred to by index or key.
Sets are unchangeable, meaning that we cannot change the items after the set has
been created.
Once a set is created, you cannot change its items, but you can add new items.
O/P:
{'Friday','Tuesday','Monday','Thursday',,'Wednesday'}
<class 'set'>
looping through the set elements ...
Friday
Tuesday
Monday
Thursday
Wednesday
Set Methods:
Method Description
add() Adds an element to the set
clear() Removes all the elements from the set
copy() Returns a copy of the set
Returns a set containing the difference between two or
difference()
more sets
Removes the items in this set that are also included in
difference_update()
another, specified set
discard() Remove the specified item
intersection() Returns a set, that is the intersection of two other sets
intersection_update() Removes the items in this set that are not present in
other, specified set(s)
isdisjoint() Returns whether two sets have a intersection or not
issubset() Returns whether another set contains this set or not
issuperset() Returns whether this set contains another set or not
pop() Removes an element from the set
remove() Removes the specified element
symmetric_difference() Returns a set with the symmetric differences of two sets
symmetric_difference_ inserts the symmetric differences from this set and
update() another
union() Return a set containing the union of sets
update() Update the set with the union of this set and others
To determine how many items a set has, use the “len()” method.
To add items from another set into the current set, use the “update()” method.
O/P: {'pineapple','banana','mango','papaya','apple','cherry'}
The object in the “update()” method does not have be a set, it can be any iterable
object (tuples, lists, dictionaries etc.).
Note: If the item to remove does not exist, remove() will raise an error.
You can also use the “pop()” method to remove an item, but this method will
remove the last item. Remember that sets are unordered, so you will not know what
item that gets removed.
The “intersection()” method will return a new set, that only contains the items that
are present in both sets.
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
print(z) #{'apple'}
The “symmetric_difference_update()” method will keep only the elements that
are NOT present in both sets.
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.symmetric_difference_update(y)
print(x) # {'microsoft', 'cherry', 'banana', 'google'}
The “symmetric_difference()” method will return a new set, that contains only
the elements that are NOT present in both sets.
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.symmetric_difference(y)
print(z) # {'google', 'microsoft', 'banana', 'cherry'}
FrozenSets
The frozen sets are the immutable form of the normal sets, i.e., the items of the
frozen set cannot be changed and therefore it can be used as a key in the dictionary.
We cannot change or append the content of the frozen sets by using the methods
like add() or remove().
The frozenset() method is used to create the frozenset object. The iterable sequence
is passed into this method which is converted into the frozen set as a return type of the
method.
Consider the following example to create the frozen set.
Frozenset = frozenset([1,2,3,4,5])
print(type(Frozenset))
for i in Frozenset:
print(i, end=’ ‘);
Frozenset.add(6)
#gives an error since we cannot change the content of Frozenset after creation