Excercises On Sets
Excercises On Sets
3. Remove empty sets from a list of sets and count how many were removed.
Hint:
o Use a while loop to iterate through the list. Check the length of
each set using len() and remove empty sets using del. Keep a
counter to track how many sets are removed.
o Key functions: len(), del
o Example: if len(L1[i]) == 0: del L1[i]
PROGRAMS
1.
s = {1, 2, 3, frozenset({4, 5}), 6}
a=1
for i in s:
if isinstance(i, frozenset):
a=0
break
if a == 0:
print('Yes')
else:
print('No')
2.
4.
L1 = [1, {2, 3}, 4, {5, 6}, 7]
a=0
for i in L1:
if isinstance(i, set):
a += 1
print("Number of sets:", a)
5.
L1 = [{3, 1}, {4, 6, 5}, {9, 7}]
L2 = sorted(set().union(*L1))
print(L2)