Python - Set Methods and Operations to Solve Common Problems
** Set Methods and Operations to Solve Common Problems
==========================================
s1 = set([1, 2, 3, 4, 5])
print(s1)
---------------------------------
{1, 2, 3, 4, 5}
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
print(s1)
---------------------------------
{1, 2, 3, 4, 5}
---------------------------------
==========================================
s1 = set()
print(s1)
---------------------------------
set()
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5, 1, 2, 3}
print(s1)
---------------------------------
{1, 2, 3, 4, 5}
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
s1.add(6)
print(s1)
---------------------------------
{1, 2, 3, 4, 5, 6}
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
s1.update([6, 7, 8])
print(s1)
---------------------------------
{1, 2, 3, 4, 5, 6, 7, 8}
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
s2 = {7, 8, 9}
s1.update([6, 7, 8], s2)
print(s1)
---------------------------------
{1, 2, 3, 4, 5, 6, 7, 8, 9}
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
s1.remove(5)
print(s1)
---------------------------------
{1, 2, 3, 4}
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
s1.remove(6)
print(s1)
---------------------------------
s1.remove(6)
KeyError: 6
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
s1.discard(6)
print(s1)
---------------------------------
{1, 2, 3, 4, 5}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s1.intersection(s2)
print(s4)
---------------------------------
{2, 3}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s1.intersection(s2, s3)
print(s4)
---------------------------------
{3}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s1.difference(s2)
print(s4)
---------------------------------
{1}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s2.difference(s1)
print(s4)
---------------------------------
{4}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s2.difference(s1, s3)
print(s4)
---------------------------------
set()
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s3.difference(s1, s2)
print(s4)
---------------------------------
{5}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s1.symmetric_difference(s2)
print(s4)
---------------------------------
{1, 4}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s2.symmetric_difference(s1)
print(s4)
---------------------------------
{1, 4}
---------------------------------
==========================================
l1 = [1, 2, 3, 1, 2, 3]
l2 = list(set(l1))
print(l2)
---------------------------------
[1, 2, 3]
---------------------------------
==========================================
employees = ['Corey', 'Jim', 'Steven', 'April', 'Judy', 'Jenn', 'John', 'Jane']
gym_members = ['April', 'John', 'Corey']
developers = ['Judy', 'Corey', 'Steven', 'Jane', 'April']
result = set(gym_members).intersection(developers)
print(result)
---------------------------------
{'Corey', 'April'}
---------------------------------
==========================================
employees = ['Corey', 'Jim', 'Steven', 'April', 'Judy', 'Jenn', 'John', 'Jane']
gym_members = ['April', 'John', 'Corey']
developers = ['Judy', 'Corey', 'Steven', 'Jane', 'April']
result = set(employees).difference(gym_members, developers)
print(result)
---------------------------------
{'Jenn', 'Jim'}
---------------------------------
==========================================
employees = ['Corey', 'Jim', 'Steven', 'April', 'Judy', 'Jenn', 'John', 'Jane']
gym_members = ['April', 'John', 'Corey']
developers = ['Judy', 'Corey', 'Steven', 'Jane', 'April']
if 'Corey' in developers:
print('Found!')
# O(n) for list
# O(1) for a set
---------------------------------
Found!
---------------------------------