Set theory
Set theory in mathematics deals with groups of objects. It describes the relationship of an object with a set or group. Python also implements the set as standard python module. The sets module provides classes for constructing and manipulating unordered collections of unique elements.
Common uses include membership testing, removing duplicates from a sequence, and computing standard math operations on sets such as intersection, union, difference, and symmetric difference.
Non operator versions like union(), intersection(), difference(), symmetric_difference(), issubset(), issuperset() can take any iterable as argument. So it is recommended to use the non operator versions to be explicit and better readability.
Creating a set
set can be created using {} or by using set(), while working with set always use ‘set’ to be explicit and better readability.
developers = {'Alex', 'Dinesh', 'Rajesh', 'Maya', 'Suresh', 'Yuva'}
developers_list = ['Alex', 'Dinesh', 'Rajesh', 'Maya', 'Suresh', 'Yuva']
developers = set(developers_list) # set method takes an iterable
php_developers = set(['Dinesh', 'Rajesh', 'Maya', 'Suresh'])
python_developers = set(['Maya', 'Siva', 'Suresh', 'Yuva'])
Now, we have two sets named php_developers
and python_developers
.
Basic operations
# Total items in the set
total_members = len(python_developers)
print(f"{total_members} people knows Python")
# Loop through items in the set
for dev in python_developers:
print(dev)
# Check if a item is present in the set
if 'Maya' in python_developers:
print('Maya is a member of python_developers')
# Check if a item is not present in the set
if 'Shri' not in python_developers:
print('Shri is not a member of python_developers')
Union operation using set
Union gives the developers who knows php or python or both.
# people who knows php or python or both
# using union() method
knows_php_or_python_or_both = php_developers.union(python_developers)
print(knows_php_or_python_or_both)
# using | operator
knows_php_or_python_or_both = php_developers | python_developers
print(knows_php_or_python_or_both)
Intersection operation using set
Intersection gives only the developers who knows both php and python.
# People who knows both php and python
# using intersection() method
both_php_and_python = php_developers.intersection(python_developers)
print(both_php_and_python)
# using & operator
both_php_and_python = php_developers & python_developers
print(both_php_and_python)
Difference operation using set
Difference of php developers and python developers gives only the developers who knows php.
# people who knows only php
# using difference() method
diff = php_developers.difference(python_developers)
print(diff)
# using - operator
diff = php_developers - python_developers
print(diff)
Symmetric Difference operation using set
Symmetric difference gives the developers who knows only php or python but not both.
using symmetric_difference() method
symmetric_diff = php_developers.symmetric_difference(python_developers)
print(symmetric_diff)
using ^ operator
symmetric_diff = php_developers ^ python_developers
print(symmetric_diff)
Putting it all together
php_developers = set(['Dinesh', 'Rajesh', 'Maya', 'Suresh'])
python_developers = set(['Maya', 'Siva', 'Suresh', 'Yuva'])
# Total items in the set
total_members = len(python_developers)
print(f"{total_members} people knows Python")
# Loop through items in the set
for dev in python_developers:
print(dev)
# Check if a item is present in the set
if 'Maya' in python_developers:
print('Maya is a member of python_developers')
# Check if a item is not present in the set
if 'Shri' not in python_developers:
print('Shri is not a member of python_developers')
# using union() method
knows_php_or_python_or_both = php_developers.union(python_developers)
print(knows_php_or_python_or_both)
# using | operator
knows_php_or_python_or_both = php_developers | python_developers
print(knows_php_or_python_or_both)
# using intersection() method
both_php_and_python = php_developers.intersection(python_developers)
print(both_php_and_python)
# using & operator
both_php_and_python = php_developers & python_developers
print(both_php_and_python)
# people who knows only php
# using difference() method
diff = php_developers.difference(python_developers)
print(diff)
# using - operator
diff = php_developers - python_developers
print(diff)
symmetric_diff = php_developers.symmetric_difference(python_developers)
print(symmetric_diff)
symmetric_diff = php_developers ^ python_developers
print(symmetric_diff)
Tips and tricks
Find unique letters in a string
set('hello world') # set([' ', 'e', 'd', 'h', 'l', 'o', 'r', 'w'])
Remove / find duplicates in a list/tuple
l = ['a', 'b', 'c', 'd', 'a', 'b']
set(l) # set(['a', 'c', 'b', 'd'])
Check if subset
issubset() checks if all items in the set exists in other set or an iterable.
# is subset
# s.issubset(t) test whether every element in s is in t
# using issubset() method
is_subset = python_developers.issubset(php_developers)
print is_subset
# using <= operator
is_subset = python_developers <= php_developers
print is_subset
Check if superset
issuperset() checks if all items in given iterable exists in the set.
# is superset
# s.issuperset(t) - test whether every element in t is in s
# using issuperset() method
is_superset = python_developers.issuperset(php_developers)
print is_superset
# using >= operator
is_superset = python_developers >= php_developers
print is_superset
Check if a list exists in another list
list1 = ['a', 'b']
set1 = set(list1)
list2 = ['a', 'b', 'c']
set1.issubset(list2) # all items in list1 exists in list2
Originally posted on idiotinside.com
Have any questions or suggestions? Please comment below. Learnt something new? Share this article with others who may find this useful. Stay tuned for upcoming articles. Subscribe to the newsletter and Connect with me on twitter to get my future articles.