Sets#

Set is a collection of items. Let me take you back to your elementary or high school Mathematics lesson. The Mathematics definition of a set can be applied also in Python. Set is a collection of unordered and un-indexed distinct elements. In Python set is used to store unique items, and it is possible to find the union, intersection, difference, symmetric difference, subset, super set and disjoint set among sets.

Creating a Set#

s = set()
s
set()
s = {"item1", "item2", "item3", "item4"}
s
{'item1', 'item2', 'item3', 'item4'}
fruits = {"banana", "orange", "mango", "lemon"}
fruits
{'banana', 'lemon', 'mango', 'orange'}

Getting Set’s Length#

s = {"item1", "item2", "item3", "item4"}
len(s)
4
fruits = {"banana", "orange", "mango", "lemon"}
len(fruits)
4

Accessing Items in a Set#

We use loops to access items. We will see this in loop section.

Checking an Item#

s = {"item1", "item2", "item3", "item4"}
print("Does set s contain item3?", "item3" in s)
Does set s contain item3? True
fruits = {"banana", "orange", "mango", "lemon"}
print("mango" in fruits)
True

Adding Items to a Set#

add()#

s = {"item1", "item2", "item3", "item4"}
s.add("item5")
s
{'item1', 'item2', 'item3', 'item4', 'item5'}
fruits = {"banana", "orange", "mango", "lemon"}
fruits.add("lime")
fruits
{'banana', 'lemon', 'lime', 'mango', 'orange'}

update()#

s = {"item1", "item2", "item3", "item4"}
s.update(["item5", "item6", "item7"])
s
{'item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7'}
fruits = {"banana", "orange", "mango", "lemon"}
vegetables = ("tomato", "potato", "cabbage", "onion", "carrot")
fruits.update(vegetables)
fruits
{'banana',
 'cabbage',
 'carrot',
 'lemon',
 'mango',
 'onion',
 'orange',
 'potato',
 'tomato'}

Removing Items from a Set#

We can remove an item from a set using remove() method. If the item is not found remove() method will raise errors, so it is good to check if the item exist in the given set. However, discard() method doesn’t raise any errors.

s = {"item1", "item2", "item3", "item4"}
s.remove("item2")
s
{'item1', 'item3', 'item4'}

The pop() methods remove a random item from a list and it returns the removed item.

fruits = {"banana", "orange", "mango", "lemon"}
fruits.pop()
fruits
{'lemon', 'mango', 'orange'}

If we are interested in the removed item.

fruits = {"banana", "orange", "mango", "lemon"}
removed_item = fruits.pop()
removed_item
'banana'

Clearing Items in a Set#

s = {"item1", "item2", "item3", "item4"}
s.clear()
s
set()
fruits = {"banana", "orange", "mango", "lemon"}
fruits.clear()
fruits
set()

Deleting a Set#

s = {"item1", "item2", "item3", "item4"}
del s
fruits = {"banana", "orange", "mango", "lemon"}
del fruits

Converting List to Set#

We can convert list to set and set to list. Converting list to set removes duplicates and only unique items will be reserved.

l = ["item1", "item2", "item3", "item4", "item1", "item2"]
s = set(l)
s
{'item1', 'item2', 'item3', 'item4'}
fruits = ["banana", "orange", "mango", "lemon", "orange", "banana"]
fruits = set(fruits)
fruits
{'banana', 'lemon', 'mango', 'orange'}

Joining Sets#

union()#

s1 = {"item1", "item2", "item3", "item4"}
s2 = {"item5", "item6", "item7", "item8"}
s3 = s1.union(s2)
s3
{'item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8'}
fruits = {"banana", "orange", "mango", "lemon"}
vegetables = {"tomato", "potato", "cabbage", "onion", "carrot"}
fruits.union(vegetables)
{'banana',
 'cabbage',
 'carrot',
 'lemon',
 'mango',
 'onion',
 'orange',
 'potato',
 'tomato'}

update()#

s1 = {"item1", "item2", "item3", "item4"}
s2 = {"item5", "item6", "item7", "item8"}
s1.update(s2)
s1
{'item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8'}
fruits = {"banana", "orange", "mango", "lemon"}
vegetables = {"tomato", "potato", "cabbage", "onion", "carrot"}
fruits.update(vegetables)
fruits
{'banana',
 'cabbage',
 'carrot',
 'lemon',
 'mango',
 'onion',
 'orange',
 'potato',
 'tomato'}

Finding Intersection Items#

intersection() returns a set of items which are in both the sets.

s1 = {"item1", "item2", "item3", "item4"}
s2 = {"item3", "item2"}
s1.intersection(s2)
{'item2', 'item3'}
whole_numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
even_numbers = {0, 2, 4, 6, 8, 10}
whole_numbers.intersection(even_numbers)
{0, 2, 4, 6, 8, 10}
python = {"p", "y", "t", "h", "o", "n"}
dragon = {"d", "r", "a", "g", "o", "n"}
python.intersection(dragon)
{'n', 'o'}

Checking Subset and Super Set#

A set can be a subset or super set of other sets:

  • Subset: issubset()

  • Superset: issuperset()

s1 = {"item1", "item2", "item3", "item4"}
s2 = {"item2", "item3"}
s2.issubset(s1)
True
s1.issuperset(s2)
True
whole_numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
even_numbers = {0, 2, 4, 6, 8, 10}

print(whole_numbers.issubset(even_numbers))
print(whole_numbers.issuperset(even_numbers))
False
True
python = {"p", "y", "t", "h", "o", "n"}
dragon = {"d", "r", "a", "g", "o", "n"}
python.issubset(dragon)
False

Checking the Difference Between Two Sets#

s1 = {"item1", "item2", "item3", "item4"}
s2 = {"item2", "item3"}
s2.difference(s1)
set()
s1.difference(s2)
{'item1', 'item4'}
whole_numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
even_numbers = {0, 2, 4, 6, 8, 10}
whole_numbers.difference(even_numbers)
{1, 3, 5, 7, 9}
python = {"p", "y", "t", "o", "n"}
dragon = {"d", "r", "a", "g", "o", "n"}
print(python.difference(dragon))
print(dragon.difference(python))
{'t', 'y', 'p'}
{'g', 'd', 'a', 'r'}