0% found this document useful (0 votes)
21 views17 pages

Set & Frozen Set

Uploaded by

bhikharilal0711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views17 pages

Set & Frozen Set

Uploaded by

bhikharilal0711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Set

 A set is a collection of unique, unordered, unchangeable*, and unindexed


elements. And it automatically removes the duplicate elements.

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))

O/P: {10, 20, 30}


3

Note: Sets are unordered, so you cannot be sure in which order the items will appear.

thisset = {"apple", "banana", "cherry"}


print(thisset)

O/P: {'banana', 'apple', 'cherry'}


Set Properties:

 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.

 Sets cannot have two items with the same value.


Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}
print(Days)
print(type(Days))
print("looping through the set elements ... ")
for i in Days:
print(i)

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.

thisset = {"apple", "banana", "cherry"}


print(len(thisset))

 It is also possible to use the “set()” constructor to make a set.

thisset = set(("apple", "banana", "cherry"))


# note the double round-brackets
print(thisset)
 You can loop through the set items using a “for loop”, or ask if a specified value is
present in a set, by using the “in” keyword.

thisset = {"apple", "banana", "cherry"}


for x in thisset:
print(x)

 Check if "apple" is present in the set:

thisset = {"apple", "banana", "cherry"}


print("apple" in thisset)
 To add one item to a set use the “add()” method.

thisset = {"apple", "banana", "cherry"}


thisset.add("orange")
print(thisset)
O/P: {'cherry', 'apple', 'banana', 'orange'}

 To add items from another set into the current set, use the “update()” method.

thisset = {"apple", "banana", "cherry"}


tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)

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.).

thisset = {"apple", "banana", "cherry"}


mylist = ["kiwi", "orange"]
thisset.update(mylist)
print(thisset)
O/P: {'cherry', 'apple', 'orange', 'banana', 'kiwi'}

 To remove an item in a set, use the “remove()”, or the “discard()” method.

thisset = {"apple", "banana", "cherry"}


thisset.remove("apple")
print(thisset)
thisset = {"apple", "banana", "cherry"}
thisset.discard("apple")
print(thisset)

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 return value of the “pop()” method is the removed item.


thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)

 The “clear()” method empties the set:

thisset = {"apple", "banana", "cherry"}


thisset.clear()
print(thisset)
 The “del” keyword will delete the set completely:

thisset = {"apple", "banana", "cherry"}


del thisset
print(thisset) # ERROR

 There are several ways to join two or more sets in Python.


 You can use the union() method that returns a new set containing all items from
both sets, or the update() method that inserts all the items from one set into another:

set1 = {"a", "b" , "c"}


set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
Note: Both union() and update() will exclude any duplicate items.
 The “intersection_update()” method will keep only the items that are present in
both sets.

x = {"apple", "banana", "cherry"}


y = {"google", "microsoft", "apple"}
x.intersection_update(y)
print(x) #{'apple'}

 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

O/P: <class 'frozenset'>


1 2 3 4 5
Traceback (most recent call last):
File "set.py", line 6, in <module>
Frozenset.add(6) #gives an error since we can change the
content of Frozenset after creation
AttributeError: 'frozenset' object has no attribute 'add'

You might also like