Programming for Data Science¶

set¶

Dr. Bhargavi R

SCOPE, VIT Chennai

  • set is unordered collection of unique and immutable objects.
  • set is mutable data type but does not support indexing/ slicing.
  • Mathematical set operations like
    • union, intersection, symmetric difference, etc., can be performed with set objects.
  • Iterable.

Creating a set¶

In [1]:
set1 = {1, 2, 3, 4, 5}
set2 = {'string'}

print(set1)
print(set2)
{1, 2, 3, 4, 5}
{'string'}
In [2]:
week_days = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
set3      = set('string')
set4      = set(['string'])

print(week_days)
print(set3)
print(set4)
{'Tue', 'Mon', 'Sat', 'Thu', 'Fri', 'Wed', 'Sun'}
{'n', 's', 't', 'i', 'g', 'r'}
{'string'}
In [3]:
set1 = set([1,2,3,4,5])
print(set1)
{1, 2, 3, 4, 5}
In [4]:
set2 = set1
print(set2)
{1, 2, 3, 4, 5}
In [5]:
set2.add(10)
print(set1)
{1, 2, 3, 4, 5, 10}
In [6]:
set3 = set1.copy()
set3.add(20)
print(set3, set1)
{1, 2, 3, 4, 5, 20, 10} {1, 2, 3, 4, 5, 10}
In [7]:
# Empty sets must be created with the set built-in, and print in the same way
empty_set = set()
print(empty_set)
set()

Accessing Values in a Set¶

In [8]:
week_days = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])

for day in week_days:
    print(day, end=' ')
Tue Mon Sat Thu Fri Wed Sun 

Removing an Item from a Set¶

In [9]:
week_days = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
print(week_days)
{'Tue', 'Mon', 'Sat', 'Thu', 'Fri', 'Wed', 'Sun'}
In [10]:
week_days.discard("Sun")
print(week_days)
{'Tue', 'Mon', 'Sat', 'Thu', 'Fri', 'Wed'}

set operations¶

In [11]:
s1 = set(i for i in range(1,7))
print(s1)
{1, 2, 3, 4, 5, 6}
In [12]:
s2 = set(range(4,11))                   
print(s2)
{4, 5, 6, 7, 8, 9, 10}

Membership Operator¶

In [13]:
1 in s1
Out[13]:
True
In [14]:
5 in s2
Out[14]:
True
In [15]:
34 in s2
Out[15]:
False

union operation |¶

In [16]:
s_union = s1 | s2

print('using pipe symbol'.ljust(22),   s_union)
print("using union methods".ljust(22), s1.union(s2)) 
print(s1)
print(s2)
using pipe symbol      {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
using union methods    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{1, 2, 3, 4, 5, 6}
{4, 5, 6, 7, 8, 9, 10}

intersection operation &¶

In [17]:
s_intersection = s1 & s2
print(s_intersection)
{4, 5, 6}
In [18]:
s_intersection = s1.intersection(s2)
print(s_intersection)
print(s1, s2)

s1.intersection_update(s2)
print(s1)
{4, 5, 6}
{1, 2, 3, 4, 5, 6} {4, 5, 6, 7, 8, 9, 10}
{4, 5, 6}

More Operations¶

In [19]:
# Set Difference
s1     = {1, 2, 3, 4, 5}
s2     = {4, 5, 6}
s_diff = s1 - s2 # set difference. Elements in s1 but not in s2
print(s_diff)
{1, 2, 3}
In [20]:
# Symmetric Difference
s1 = {1, 2, 3, 4, 5}
s2 = {4, 5, 6, 7, 8}

# Elements in s1 and s2 but not in both (excluding the intersection)
sym_diff = s1.symmetric_difference(s2) 

print(sym_diff)
{1, 2, 3, 6, 7, 8}