Sets in Python
Sets in Python
Output:
{1, 2, 3, 4}
Iteration Over Sets
Ex:
s=set([1,2,3,4,1,2,1]) #we can make set from list
for i in s:
print(i)
Output:
1
2
3
4
Adding members in set
We can add single element using the add() method and
multiple elements using the update() method. The update()
method can take tuples, lists, strings or other sets as its argument.
The elements of the iterable are added to the set.
Ex:
s=set()
s.add("Red")
print(s)
s.update(["Blue", "Green"])
print(s)
Output:
{'Red'}
{'Red', 'Blue', 'Green'}
Ex:
s={1,2,3,4,"helllo",(1,2,3)}
s.update([4,5,6],{1,7,8})
print(s)
Output:
{1, 2, 3, 4, 5, 6, 7, 8, (1, 2, 3), 'helllo'}
Output:
{'blue'}
Union of sets
Ex:
setx = set(["green", "blue"])
sety = set(["blue", "yellow"])
setz = setx | sety
print(setz)
#print(setx|sety))
#print(setx.union(sety))
Output:
{'green', 'blue', 'yellow'}
Set difference
If A and B are two sets. The set difference
of A and B is a set of elements that exists only in
set A but not in B.
Ex:
setx = set(["green", "blue"])
sety = set(["blue", "yellow"])
setz = setx - sety
print(setz)
#print(setx - sety)
#print(setx.difference(sety))
Output:
{'green'}
issubset and issuperset
The issubset() method returns True if all elements of a set are present in another
set (passed as an argument). If not, it returns False.
The issuperset() method returns True if all items in the specified set exists in the
original set, otherwise it retuns False.
Ex:
setx = set(["green", "blue"])
sety = set(["blue", "yellow", "green"])
sub = setx <= sety
print(sub)
sup = setx >= sety
print(sup)
Output:
True
False
issubset and issuperset
The issuperset() method returns True if all items in the specified set
exists in the original set, otherwise it retuns False.
Ex:
x = {"f", "e", "d", "c", "b", "a"}
y = {"a", "b", "c"}
z = x.issuperset(y)
print(z)
Output:
True
Isdisjoint()
The isdisjoint() method returns True if two sets are disjoint
sets. If not, it returns False.
EX:
A={1,2,3,4}
B={3,4}
print(A.issubset(B))
print(A.issuperset(B))
print(A.isdisjoint(B))
Output:
False
True
False
Shallow copy of sets
Ex:
setx = set(["green", "blue"])
sety = set(["blue", "yellow", "green"])
c = setx.copy()
print(c)
Output:
{'blue', 'green'}
Set Membership Test
We can test if an item exists in a set or not,
using the keyword in.
Ex:
A = set("apple")
print('a' in A)
print('p' not in A)
Output:
True
False
Built-in Functions with Set
Frozenset
Frozenset is has the characteristics of a
set, but its elements cannot be changed
once assigned.
While tuples are immutable lists, frozensets
are immutable sets.
Frozensets can be used as keys
in Dictionary or as elements of another
set.
Frozensets can be created using the
function frozenset().
Ex:
A = frozenset("apple")
This datatype supports methods like
copy(), difference(), intersection(),
isdisjoint(), issubset(), issuperset(),
symmetric_difference() and union().
Being immutable it does not have method
that add or remove elements.
# random dictionary
person = {"name": "John", "age": 23, "sex": "male"}
fSet = frozenset(person)
print('The frozen set is:', fSet)
Output: