
Python Set
Python Sets
Sets are an unordered collection of unique elements. We can construct them by using the set() function. Let’s go ahead and make a set to see how it works.
Set and Booleans
There are two other object types in Python that we should quickly cover:
- Sets
- Booleans
Creating a Set
Sets can be created with set constructor or by directly defining the values in a {} brackets
a1 = set(),#list() b2 = {1,2,3} print(type(a1) print(type(b2)) <class 'tuple'> <class 'set'>
Various methods in Set
- membership “in”
- isdisjoint
- subset
- propersubset
- superset
- proper superset
- union
- intersection
- difference
- intersection_update
- difference_update
- symmetric_difference_update
- copy
- add
- pop
- remove
- discard
- clear
1. membership “in”
Examplea = {1,2,3,4,5} #a=1,2 #print(type(a)) b= {4,5,6} c ={6,7,8} d= {6,7,9,10,11 } e={0,1,6,7,11,22,33} f= {1,2,3,6,7,8} ab ={1,2, 3, 4, 5} aa= {“ab”,”bc”,”de”} z =4 #print(len(a)) print(z in b) print(“######################”) #checking conditions print(“checking membership of c in ab code: c not in ab : result ={0}”.format(z not in ab)) print(“######################”) True ###################### checking membership of c in ab code: c not in ab : result =False ###################### |
2.Isdisjoint
Exampleprint(“checking if set a and ab are disjoint a is disjoint to ab code: a.isdisjoint(ab):result: {0} “.format(a.isdisjoint(ab))) print(“######################”) checking if set a and ab are disjoint a is disjoint to ab code: a.isdisjoint(ab): result: False ###################### |
3. subset
Exampleprint(“check if all the members of a are members of set ab code:- ################ |
4. propersubset
print(“check if all the members of a are members of set ab, here ab is called as superset of a code: ab.issuperset(a) : result – {0} “.format(ab.issuperset(a))) print(“check if all the members of a are members of set ab, here ab is called as superset of a, using symbol code: ab >= a result: – {0}”.format(ab >= a)) print(“######################”) print(“check if all the members of a are members of set ab, here ab is called as superset of a, and is a proper superset code: ab > a result: – {0}”.format(ab > a)) |
5.Union
Exampleprint(“Union or combining elements from two sets code: a.union(b) : result: {}”.format(a.union(b))) print(“Union or combining elements from two sets – using symbol code: a|b :result: {}”.format(a|b)) print(“######################”) |
6.Intersection
Exampleprint(“finding the common items between two sets code: a.intersection(b) result: – {0}”.format(a.intersection(b))) print(“finding the common items between two sets -using symbol code: a & b result: – {0}”.format(a&b)) print(“######################”) |
7.Difference
Exampleprint(“find the difference between two sets code: a.difference(b) {0}”.format(a.difference(b))) print(“find the difference between two sets, a -b code: a-b : result: – {0} “.format(a-b)) #print(“udpating one set with elements in other set code: c.update(b) – result {0}”.format(c.update(b))) print(“######################”) #c|=b print(“C set is {0} “.format(c)) print(“D set is {0} “.format(d)) print(“intersection update between c and d: code: d.intersection_update(c) :result – {0}”.format(d.intersection_update(c))) #c &= d print(“C set is {0} “.format(c)) print(“D set is {0} “.format(d)) find the difference between two sets code: a.difference(b) {1, 2, 3} find the difference between two sets, a -b code: a-b : result: – {1, 2, 3} ###################### C set is {8, 6, 7} D set is {6, 7} intersection update between c and d: code: d.intersection_update(c) : result – None C set is {8, 6, 7} D set is {6, 7} |
8. Difference_update
Exampleprint(“C set value is {0} “.format(c)) print(“E set value is {0} “.format(e)) print(“difference updated between e and c code: e.difference_update(c) : result – {0}”.format(e.difference_update(c))) print(“E set becomes {0} “.format(e)) print(“######################”) C set value is {8, 6, 7} E set value is {0, 1, 33, 6, 7, 11, 22} difference updated between e and c code: e.difference_update(c) : result – None E set becomes {0, 1, 33, 11, 22} |
9. intersection_update
Exampleprint(“D set becomes {0} “.format(d)) print(“######################”) # #print(“intersection update between c and d: code: d.intersection_update(c) :result – {0}”.format(d.intersection_update(c))) # print(“D set becomes {0} “.format(d)) # # print(“B set value is {0} “.format(b)) print(“C set value is {0} “.format(c)) print(“D set value is {0} “.format(d)) print(“difference updated between b and d , updates the code: b.difference_update(d) : result – {0}”.format(b.difference_update(d))) print(“B set becomes {0} “.format(b)) #print(“difference updated between b and c , updates the using the symbol: b -= c : result – {0}”.format(b -= c)) print(“B set becomes {0} “.format(b)) print(“######################”) D set becomes {6, 7} ###################### B set value is {4, 5, 6} C set value is {8, 6, 7} D set value is {6, 7} difference updated between b and d , updates the code: b.difference_update(d) : result – None B set becomes {4, 5} B set becomes {4, 5} ################ |
10. symmetric_difference_update
Exampleprint(“F set becomes {0} after symmetric difference”.format(f)) |
11.Copy
Exampleprint(aa) |
12. Add
Exampleaa.add(“xy”) |
13. pop
Exampleaa.pop() |
14. Remove
Exampleaa.remove(“ab”) KeyError: ‘ab’ |
15. Discard
Exampleaa.discard(“abc”) |
16. Clear
ExampleIn [153]: |