My Coding > Programming language > Python > Python SETs HowTo

Python SETs HowTo

About SETs in Python

Set in Pythons is a collection which is unordered and unindexed without duplicated members. Set can store any type of variables and mixture of types. Also all elements in Set are unchangeable. It is possible to add or remove element, but not modify it

How To Create a Set. Creating a Set in Python

It is possible to create empty Set and add elements. Convert list into a Set or create Set with elements. Duplicated elements will be removed


a=[1,1,2,3,4,5]     # list with 6 elements
b=[4,4,5,6,7,8]     # list with 6 elements
E = set()           # empty set
L = {1, 2, 3, 4, 5} # {1, 2, 3, 4, 5}
A = set(a)          # {1, 2, 3, 4, 5}
B = set(a)          # {4, 5, 6, 7, 8}
C = set(a + b)      # {1,2,3,4,5,6,7,8}

How to Add one element to SET

It is possible to add one element to SET in Python by using add() method or union method:


A={1, 2, 3, 4, 5}    # SET with 5 elements
B={1, 2, 3, 4, 5}    # SET with 5 elements
n = 6                # element to be added
# adding one lement only:
A.add(n)             # {1, 2, 3, 4, 5, 6}
# adding one elemen via list
B=B.union([n])       # {1, 2, 3, 4, 5, 6}

How to Add list to SET

It is possible to add list to the SET in Python by few different techniques:

.union() method to add LIST to SET


A={1, 2, 3}          # SET with 3 elements
L=[3, 4, 5]          # LIST with 3 elements
A=A.union(L)         # {1, 2, 3, 4, 5}

.update() method to add LIST to SET


A={1, 2, 3}          # SET with 3 elements
L=[3, 4, 5]          # LIST with 3 elements
A.update(L)         # {1, 2, 3, 4, 5}

|= command to add LIST converted to set to SET


A={1, 2, 3}          # SET with 3 elements
L=[3, 4, 5]          # LIST with 3 elements
A |= set(L)          # {1, 2, 3, 4, 5}
# which is equivalent to
A = A | set(L)

And we are now can see in details how to join two sets together

How to combine SETs in Python (unite)

It is possible to make union of, or combine two or more sets


A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
C = (7, 8, 9, 10, 11}
AB1=set.union(A,B)          # {1, 2, 3, 4, 5, 6, 7, 8}
AB2 = A.union(B)            # {1, 2, 3, 4, 5, 6, 7, 8}

ABC1 = set.union(A, B, C)   # {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
ABC2 = A.union(B.union(C))  # {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

How to subtract one SET from another

To remove or subtract one SET from another - it is possible to use ariphmetical subtraction


A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
C = A-B               # {1, 2, 3}

How to fing overlapping and non-overlapping subsets

Calculate overlapped (intersection) and non-overlapped parts of sets is very easy


A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
ABi = A.intersection(B)        # {4, 5} - this is overlapping, common for two sets area
ABn=set.union(A, B) - ABi      # {1, 2, 3, 6, 7, 8} - non-overlapping area.
ABn=A.symmetric_difference(B)  # {1, 2, 3, 6, 7, 8} - non-overlapping area.


Published: 2021-09-15 07:55:14
Updated: 2021-09-23 02:06:27

Last 10 artitles


9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.