Python Sets
Python Sets
What is Set
• A set is a collection which is
• Unordered : cannot be referred to by index
• do not allow duplicate values.
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]
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.
OUTPUT:
139748080630688
139748080630688
{'cherry', 'orange', 'banana', 'apple'}
Add sets
• union():returns a new set containing all items
from both sets,
OUTPUT:
{1, 2, 3, 'c', 'b', 'a'}
Add sets..union
• Alternatively, we may use the union operator ‘|’ to achieve the same
result
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.
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
OUTPUT:
{'cherry', 'banana'}
{'microsoft', 'google'}
Sets..difference
• Another way of doing same thing through operator ‘-’
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
OUTPUT:
{'b', 'c'}
Sets..symmetric_difference
• To get elements which are in one of the either sets but not in both
OUTPUT:
{'cherry', 'microsoft', 'google', 'banana'}
Add sets..update
• To add items from another set into the current set, use the update()
method.
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.).
OUTPUT:
{'orange', 'kiwi', 'apple', 'cherry', 'banana'}
Remove Set Items
• To remove an item in a set, use the remove() method.
OUTPUT:
{'cherry', 'apple'}
Remove Set Items..
• remove() method will generate error if item to be removed doesn’t exist.
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’}
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.
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.
OUTPUT:
set()
Remove Set..del()
• To remove THE SET ITSELF, use del.
OUTPUT:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-b0ed94c1bb6f> in <module>
3 del thisset
4
----> 5 print(thisset)
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.