0% found this document useful (0 votes)
14 views

Python Sets

Uploaded by

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

Python Sets

Uploaded by

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

Sets

What is Set
• A set is a collection which is
• Unordered : cannot be referred to by index
• do not allow duplicate values.

• Sets are written with curly brackets


What is Set..
• Example:
thisset = {"apple", "banana", "cherry"}
print(thisset)
print(len(thisset))
OUTPUT: {'cherry', 'banana', 'apple’}
3

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


print(thisset)
print(len(thisset))
OUTPUT: {'cherry', 'banana', 'apple'}
What is Set..
Set items can be of any data type

• String, int and boolean data types:


set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}

•A set can contain different data types:


set1 = {"abc", 34, True, 40, "male"}
access set elements
Items in a set CAN NOT BE ACCESSED by referring to an index or a key.
set1 = {"apple", "banana", "cherry"}
print(set1)
set1[0]

OUTPUT:
{'cherry', 'banana', 'apple'}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-9c35aa9afb04> in <module>
1 set1 = {"apple", "banana", "cherry"}
2 print(set1)
----> 3 set1[0]

TypeError: 'set' object is not subscriptable


access set elements through loop
Loop through the set,
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)

OUTPUT:
cherry
banana
apple
access set element through
‘in’
Check if a specified value is present in a set, by using the in keyword
thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)

OUTPUT:
True
Add items to set
• Once a set is created, you cannot change its items, but you can add new
items.
• To add one item to a set use the add() method.

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


print(id(thisset))
thisset.add("orange")
print(id(thisset))
print(thisset)

OUTPUT:
139748080630688
139748080630688
{'cherry', 'orange', 'banana', 'apple'}
Add sets
• union():returns a new set containing all items
from both sets,

• update(): inserts all the items from one set into


another.

• Both union() and update() will exclude any


duplicate items.
Add sets..union
• To add items from another set into the current set, use the update()
method.

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


set2 = {1, 2, 3,”a”}
set3 = set1.union(set2)
print(set3)

OUTPUT:
{1, 2, 3, 'c', 'b', 'a'}
Add sets..union
• Alternatively, we may use the union operator ‘|’ to achieve the same
result

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


set2 = {1, 2, 3,”a”}
set3 = set1 | (set2)
print(set3)

OUTPUT:
{1, 2, 3, 'c', 'b', 'a'}
Union of 3 sets
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3,"a"}
set3 = {"d",11,12,13,"a"}
print(set.union(set1,set2,set3))

OUTPUT:
{1, 2, 3, 11, 'd', 12, 'c', 'a', 13, 'b'}
Sets..intersection
• keep only the items that are present in both sets.

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


y = {"google", "microsoft", "apple"}
z = x.intersection(y)
print(z)

OUTPUT:
{'apple'}
Intersection of 3 sets
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3,"a"}
set3 = {"d",11,12,13,"a"}
print(set.intersection(set1,set2,set3))

OUTPUT:
{'a’}
Sets..difference
• To get what is left after common elements between two sets are
removed from the specified set

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


y = {"google", "microsoft", "apple"}
zx = x.difference(y)
zy = y.difference(x)
print(zx)
print(zy)

OUTPUT:
{'cherry', 'banana'}
{'microsoft', 'google'}
Sets..difference
• Another way of doing same thing through operator ‘-’

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


y = {"google", "microsoft", "apple"}
zx = x-y
zy = y-x
print(zx)
print(zy)

OUTPUT:
{'cherry', 'banana'}
{'microsoft', 'google'}
Difference of 3 sets
• It takes union of all sets except first one and subtract the union from the
first set

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


set2 = {1, 2, 3,"a"}
set3 = {"d",11,12,13,"a"}
print(set. difference(set1,set2,set3))

OUTPUT:
{'b', 'c'}
Sets..symmetric_difference
• To get elements which are in one of the either sets but not in both

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


y = {"google", "microsoft", "apple"}
z = x.symmetric_difference(y)
print(z)

OUTPUT:
{'cherry', 'microsoft', 'google', 'banana'}
Add sets..update
• 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)

OUTPUT:
{'cherry', 'papaya', 'pineapple', 'banana', 'mango', 'apple'}
Add sets..update
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya","apple"}
thisset.update(tropical)
print(thisset)
for item in thisset:
print(item)
print(len(thisset))

OUTPUT:
???
Add sets.. update
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya","apple"}
thisset.update(tropical)
print(thisset)
for item in thisset:
print(item)
print(len(thisset))

OUTPUT:
{'cherry', 'papaya', 'pineapple', 'banana', 'mango', 'apple’}
cherry
papaya
pineapple
banana
mango
apple
Add list, tuple, dictionary etc
• The object in the update() method does not have to 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)

OUTPUT:
{'orange', 'kiwi', 'apple', 'cherry', 'banana'}
Remove Set Items
• To remove an item in a set, use the remove() method.

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


thisset.remove("banana")
print(thisset)

OUTPUT:
{'cherry', 'apple'}
Remove Set Items..
• remove() method will generate error if item to be removed doesn’t exist.

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


thisset.remove("banana")
print(thisset)
thisset.remove("banana")

OUTPUT:
{'cherry', 'apple'}
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-5-6cb521baab94> in <module>
2 thisset.remove("banana")
3 print(thisset)
----> 4 thisset.remove("banana")

KeyError: 'banana'
Remove Set Items..Discard()
• If you don’t want error due to non-existence of item to be removed, USE
DISCARD
thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)
thisset.discard("banana")

OUTPUT:
{'cherry', 'apple’}

NOTE: NO Error Generated


Remove Set Items..pop()
• What if pop() is used to remove items..
• remove the last item
thisset = {"apple", "banana", "cherry","mango"}
x = thisset.pop()
print(x)
print(thisset)

OUTPUT:
?????
Remove Set Items..pop()
• Remember, set is unordered , so there is no fixed last item. Any item can
be last.
• So we can’t be sure which item shall be removed.

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


x = thisset.pop()
print(x)
print(thisset)

OUTPUT:
ONE OF THE OUTPUTS:
cherry {'banana', 'mango', 'apple'}
Remove Set Items..clear()
• To remove all items in a set (BUT NOT THE SET ITSELF), use clear.

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


thisset.clear()
print(thisset)

OUTPUT:
set()
Remove Set..del()
• To remove THE SET ITSELF, use del.

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


del thisset
print(thisset)

OUTPUT:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-b0ed94c1bb6f> in <module>
3 del thisset
4
----> 5 print(thisset)

NameError: name 'thisset' is not defined


Finding min/max/sum and
length
• The function min(), max(), sum() and len work for sets in the same
manner as for lists.

min() Function

digits=set((0,1,2,3,4,5,6,7,8,9))
print(min(digits))

OUTPUT:
0
Finding min/max/sum and length

max() Function

digits=set((0,1,2,3,4,5,6,7,8,9))
print(max(digits))

OUTPUT:
9
Finding min/max/sum and length

sum() Function

digits=set((0,1,2,3,4,5,6,7,8,9))
print(sum(digits))

OUTPUT:
45
Finding min/max/sum and length

len() Function

digits=set((0,1,2,3,4,5,6,7,8,9))
print(len(digits))

OUTPUT:
10
Exercise
Exercise
THANK YOU
•List is a collection which is ordered and changeable.
Allows duplicate members.
•Tuple is a collection which is ordered and
unchangeable. Allows duplicate members.
•Set is a collection which is unordered, unchangeable*,
and unindexed. No duplicate members.
•Dictionary is a collection which is ordered** and
changeable. No duplicate members.

You might also like