The set can contain elements or members of anything: numbers, characters, words, names, letters of the alphabet, even other sets and tuples. Sets are usually denoted with capital letters. Set is a data type in python and can hold any number of elements and any type of data in it. Even set data type is similar to other data types in python like list and tuple still there are some differences.
- Only unique object will be inserted as set element, duplicate or repeated elements will be removed from set object.
- Set object can not have list and dictionary object as element but we can pass a single list or dictionary object to Set() method and turn the list or dictionary into individual elements. This is the shortest method to remove the duplicates from list object. No need to filter the duplicate from dictionary object as it is already taken in care by dictionary.
- It is wise and error free to have tuple object as set's elements.
- Even the set contain the immutable object(s) as element still the set object is mutable and can be altered at run time.
- Set can contain another set as element.
Let move to programming examples:-
Using set() method to create set object.
Using set() method to create set object.
1 2 3 |
set1 = set("this is python hunter") print(set1)
|
We can pass list to built in set() method.
1 2 3 |
set1 = set(["this","is","python","python","hunter","list"]) print(set1)
|
Passing tuple to built in set() method.
1 2 3 4 |
set1 = set(("this","is","python","python","hunter","tuple"))
print(set1)
|
We can not add list as element.
1 |
set1 = set((["this","is","list"],["this","is","another list"]))
|
But we can add tuple as element.
1 2 |
set1 = set((("this","is","list"),("this","is","another list"))) print(set1)
|
Set are mutable even the set carry immutable object.
1 2 3 4 |
set1 = set([1,2,4]) print(set1) set1.add("this") print(set1)
|
The above problem can be solved with frozenset method. Frozenset will not let the user to add any element or alter the set object.
1 2 3 4 |
set1 = frozenset([1,2,4]) print(set1) set1.add("this") print(set1)
|
AttributeError: 'frozenset' object has no attribute 'add'
Clearing set:-
1 2 3 |
set1 = {"this","is","python","hunter","simple","notation","for","set"} print(set1) set1.clear()
|
Copy Set:-
1 2 3 4 5 6 |
set1 = {"copy","set","elements"} setCopy = set1 print(set1) print(setCopy) setCopy.clear() print(set1)
|
setCopy is new var and pointing to original set1. both set1 and setCopy are pointing to same memory location so clearing either setObject will affect the other one. This is the concept of Shallow and Deep copy. Click here to learn more.
The above problem can be solved with copy method.
1 2 3 4 5 6 7 |
print("*"*20) set1 = {"copy","set","elements"} setCopy = set1.copy() print(set1) print(setCopy) setCopy.clear() print(set1)
|
in this case only the setCopy is cleared and set1 is no affected any more.
Take out the differece b/w two sets.
1 2 3 4 |
#Take out the differece b/w two sets set1 = {'a','b','c','d','e'} set2 = {'p','l','k','j','g'} set3 = {'k','l','d','f','g'}
|
1 |
print(set1.difference(set2))
|
1 2 3 4 5 6 |
print(set2.difference(set1)) # this can also be performed with '-' minus operator.
print(set1 - set2)
print(set2 - set1) print((set1 - set2) - set3)
|
difference_update()
1 2 3 4 |
set1 = {'a','b','c','d','e'} set2 = {'b','c'} set1.difference_update(set2) print(set1)
|
In above example set2 is the sub set because it has all the elements that set1 has. set1.difference_update(set2) will remove the sub set ('b','c') from set1 and the resulting set will be the remaining elements e.g ('a','d','e').
The set1.difference_update(set2) is equvelent to set1 = set1 - set2. see the example below.
1 2 3 4 |
set1 = {'a','b','c','d','e'} set2 = {'b','c'} set1 = set1 - set2 print(set1)
|
discard()
The matching elements will be removed from set, if element is not in the set then nothing will happen.
1 2 3 4 |
set1 = {'a','b','c'} set1.discard('a') # a will be removed. print(set1) set1.discard('h') # nothing will be removed.
|
intersection()
The common elements will be returned.
1 2 3 |
set1 = {'a','b','c','d','e'} set2 = {'b','c'} print(set1.intersection(set2))# b and c will be returned
|
The set1.intersection(set2) is equvelent to set1 = set1 & set2. see the example below.
1 2 3 4 |
set1 = {'a','b','c','d','e'} set2 = {'b','c'} set1 = set1 & set2 print(set1)
|
subset()
Consider subset as child, this will help you understand the concept. (please read the comments in code for better understanding)
1 2 3 4 5 6 7 8 9 10 |
set1 = {'a','b','c','d','e'} set2 = {'b','c'} print(set1.issubset(set2))# false print(set1 < set2) #false print(set2.issubset(set1))# true, because the set2 is proper subset of set1 means that the set2 contains all the elements # that set1 has. print(set2 < set1) #true print(set1 < set1) #false, always false, a set can never be a sub set of itself. print(set1 <= set1)#true because of = sign.
|
superset()
Consider subset as parent, this will help you understand the concept. (please read the comments in code for better understanding)
1 2 3 4 5 6 7 8 9 10 |
set1 = {'a','b','c','d','e'} set2 = {'b','c'} print(set1.issuperset(set2))# true print(set1 > set2) #true print(set2.issuperset(set1))# false print(set2 < set1) #true print(set1 < set1) #false, always false, a set can never be a superset set of itself. print(set1 <= set1)#true because of = sign. print(set1.issuperset(set1))# will always give true.
|
pop()
pop method will remove the element from starting of set.
1 2 3 4 |
x = {"a","b","c","d","e"} print(x) print(x.pop()) print(x.pop())
|
first the set will be sorted to acbde and then a and c will be pop out.
Is it possible to add dictionary object as set element?
ReplyDeleteNo, it's not possible, as the dictionary object is mutable.
Delete