In the previous post we learnt Python Dictionaries with code examples. In this post we will learn Python Sets in detail.
Topics Covered
Python set is an unordered collection with no duplicate elements. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
sets can be created using Curly braces {}
or using set()
function.
# sets in python
my_set_1 = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(type(my_set_1))
print(my_set_1)
my_set_2 = set(('apple', 'orange', 'apple', 'pear', 'orange', 'banana'))
print(type(my_set_2))
print(my_set_2)
<class 'set'>
{'orange', 'pear', 'banana', 'apple'}
<class 'set'>
{'orange', 'pear', 'banana', 'apple'}
Let's learn some of the most important methods for Python sets with code examples.
add()
method exampleadd()
method of python sets add an element to the set. There is no effect if the element is already present.
# sets add() method
my_set = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(my_set)
my_set.add("grapes")
print(my_set)
{'apple', 'pear', 'banana', 'orange'}
{'grapes', 'apple', 'banana', 'pear', 'orange'}
clear()
method exampleclear()
method of python sets removes all elements of the set.
# sets clear() method
my_set = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(my_set)
my_set.clear()
print(my_set)
{'apple', 'pear', 'banana', 'orange'}
set()
copy()
method examplecopy()
method of python sets returns a shallow copy of the set.
# sets copy() method
my_set = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(my_set)
my_set_1 = my_set.copy()
print(my_set_1)
{'apple', 'pear', 'orange', 'banana'}
{'apple', 'pear', 'orange', 'banana'}
difference()
method exampledifference()
method of python sets returns the difference of two or more sets as a new set. All elements
in the new set are in this set but not is other sets.
# sets difference() method
my_set_1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
my_set_2 = {4, 5}
my_set_3 = {2, 3}
my_set_4 = {9, 10}
# difference() with one set
s_diff_1 = my_set_1.difference(my_set_2)
print(s_diff_1)
# difference() with multiple sets
s_diff_2 = my_set_1.difference(my_set_2, my_set_3, my_set_4)
print(s_diff_2)
{1, 2, 3, 6, 7, 8, 9, 10}
{1, 6, 7, 8}
difference_update()
method exampledifference_update()
method of python sets removes all elements of another set from this set.
# sets difference_update() method
my_set_1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
print(my_set_1)
my_set_2 = {4, 5}
print(my_set_2)
my_set_1.difference_update(my_set_2)
print("my_set_1 after applying difference_update method")
print(my_set_1)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{4, 5}
my_set_1 after applying difference_update method
{1, 2, 3, 6, 7, 8, 9, 10}
intersection()
method exampleintersection()
method of python sets returns intersection of two sets as a new set. New set contains
elements that are in both sets.
# sets intersection() method
my_set_1 = {1, 2, 3, 4, 5, 6}
print(my_set_1)
my_set_2 = {4, 5, 6, 9, 10, 11}
print(my_set_2)
new_set = my_set_1.intersection(my_set_2)
print(new_set)
{1, 2, 3, 4, 5, 6}
{4, 5, 6, 9, 10, 11}
{4, 5, 6}
intersection_update()
method exampleintersection_update()
method of python sets updates the sets with the intersection of itself and another. i.e this set will have common elements from both sets.
# sets intersection_update() method
my_set_1 = {1, 2, 3, 4, 5, 6, 12}
print(my_set_1)
my_set_2 = {4, 5, 6, 9, 10, 11, 12, 13}
print(my_set_2)
print("my_set_1 after intersection_update method")
my_set_1.intersection_update(my_set_2)
print(my_set_1)
{1, 2, 3, 4, 5, 6, 12}
{4, 5, 6, 9, 10, 11, 12, 13}
my_set_1 after intersection_update method
{12, 4, 5, 6}
isdisjoint()
method exampleisdisjoint()
method of python sets returns True
if two sets have a null
intersection (no common elements in both of the sets).
# sets isdisjoint() method
my_set_1 = {1, 2, 3, 4, 5, 6, 12}
my_set_2 = {4, 5, 6, 9, 10, 11, 12, 13}
my_set_3 = {20, 30, 40}
my_set_4 = {40, 50}
print(my_set_1.isdisjoint(my_set_2))
print(my_set_1.isdisjoint(my_set_3))
print(my_set_2.isdisjoint(my_set_3))
print(my_set_3.isdisjoint(my_set_4))
False
True
True
False
issubset()
method exampleissubset()
method of python sets returns True
if another set contains this set.
# sets issubset() method
my_set_1 = {1, 2, 3, 4}
my_set_2 = {1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13}
my_set_3 = {1, 2, 7, 8, 9}
print(my_set_1.issubset(my_set_2))
print(my_set_1.issubset(my_set_3))
True
False
issuperset()
method exampleissuperset()
method of python sets returns True
if this set contains another set.
# sets issuperset() method
my_set_1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
my_set_2 = {1, 2, 3, 4}
my_set_3 = {10, 11, 12}
print(my_set_1.issuperset(my_set_2))
print(my_set_1.issuperset(my_set_3))
True
True
pop()
method examplepop()
method of python sets removes and returns arbitrary set element. If set is empty is raises KeyError
.
# sets pop() method
my_set_1 = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
print(my_set_1)
rm_element = my_set_1.pop()
print("Removed element: ", rm_element)
print("my_set_1 after pop:", my_set_1)
{4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
Removed element: 4
my_set_1 after pop: {5, 6, 7, 8, 9, 10, 11, 12, 13}
remove()
method exampleremove()
method of python sets removes the given element from the set. If element is not found in set it raises KeyError
.
# sets remove() method
my_set_1 = {'apple', 'pear', 'orange', 'banana'}
print(my_set_1)
# element in the set
my_set_1.remove('apple')
print(my_set_1)
my_set_1.remove('pear')
print(my_set_1)
{'orange', 'apple', 'banana', 'pear'}
{'orange', 'banana', 'pear'}
{'orange', 'banana'}
symmetric_difference()
method examplesymmetric_difference()
method of python sets returns a new set having all elements that are in exactly one of the sets..
# sets symmetric_difference() method
my_set_1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
my_set_2 = {2, 8, 9, 10, 11}
new_set = my_set_1.symmetric_difference(my_set_2)
print(new_set)
# difference between `difference and symmetric_difference` methods
print("difference vs symmetric_difference")
new_set = my_set_1.difference(my_set_2)
print(new_set)
new_set = my_set_1.symmetric_difference(my_set_2)
print(new_set)
{1, 3, 4, 5, 6, 7, 10, 11}
difference vs symmetric_difference
{1, 3, 4, 5, 6, 7}
{1, 3, 4, 5, 6, 7, 10, 11}
symmetric_difference_update()
method examplesymmetric_difference_update()
method of python updates a set with the symmetric difference of itself and another..
# sets symmetric_difference() method
my_set_1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
print("Before applying symmetric_difference_update:", my_set_1)
my_set_2 = {2, 8, 9, 10, 11}
my_set_1.symmetric_difference_update(my_set_2)
print("After applying symmetric_difference_update:", my_set_1)
Before applying symmetric_difference_update: {1, 2, 3, 4, 5, 6, 7, 8, 9}
After applying symmetric_difference_update: {1, 3, 4, 5, 6, 7, 10, 11}
update()
method exampleupdate()
method of python updates the set with the union of itself and others..
# sets update() method
my_set_1 = {5, 6, 7, 8, 9}
my_set_2 = {100, 200, 300}
print("my_set_1:", my_set_1)
print("my_set_2:", my_set_2)
my_set_1.update(my_set_2)
print("my_set_1 after update:", my_set_1)
my_set_1: {5, 6, 7, 8, 9}
my_set_2: {200, 100, 300}
my_set_1 after update: {100, 5, 6, 7, 8, 9, 300, 200}
union()
method exampleunion()
method of python returns a new set having all elements that are in either set..
# sets union() method
my_set_1 = {5, 6, 7, 8, 9}
my_set_2 = {2, 8, 9, 10, 11}
print("my_set_1:", my_set_1)
print("my_set_2:", my_set_2)
new_set = my_set_1.union(my_set_2)
print("new_set after union:", new_set)
my_set_1: {5, 6, 7, 8, 9}
my_set_2: {2, 8, 9, 10, 11}
new_set after union: {2, 5, 6, 7, 8, 9, 10, 11}