(1) What Is A Set Data Structure?
- A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements.
- Set is represented by { } (values enclosed in curly braces)
- The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set.
- This is based on a data structure known as a hash table.
- Since sets are unordered, we cannot access items using indexes as we do in lists.
Example-1
st = {1,4,2,5,1,3,3,4}
print(st)
{1, 2, 3, 4, 5}
(2) Does Converting An Object To A Set Maintain The Object’s Order?
- No. A set is not an ordered data structure, so the order is not maintained.
- Look what happens when we convert the list
[3,2,1]
to a set. It becomes{1,2,3}
.
Example-1
a = set([3,2,1])
print(a)
{1, 2, 3}
(3) Check If A Set A Subset Of Another Set.
- This can be done with the
issubset()
method.
Example-1
a = {4,5}
b = {1,2,3,4,5}
a.issubset(b)
True
(4) Check If A Set Is A Subset, Using Comparison Operators.
A set,
s1
, is a subset ofs2
if all elements ofs1
are ins2
.The operators
<=
will returnTrue
if all elements of the 1st set exist in the 2nd set (aka. is a subset).
Example-1
a = {'a','b'}
b = {'a','b','c'}
print(a <= b)
print(b <= a)
True
False
(5) Is A Set A Subset Of Itself?
Yes.
Because a set contains all elements in itself, it is indeed a subset of itself.
This is important to understand when we contrast “subset” with “proper subset” later.
Example-1
a = {10,20}
a.issubset(a)
True
(6) Check If A Specific Value Exists In A Set.
- Like other types of tables, we can check if a value exists in a set with the
in
operator.
Example-1
s = {5,7,9}
5 in s
True
(7) Check If A Value Is Not In A Set.
- We can again use the
in
operator, but this time prefaced bynot
.
Example-1
s = {'x','y''z'}
'w' not in s
False
(8) What Is The Difference Between A Subset And A Proper Subset?
- A proper subset is a subset of a set, not equal to itself.
Example-1
{1,2,3} is a proper subset of {1,2,3,4}.
{1,2,3} is not a proper subset of {1,2,3}, but it is a subset.
(9) Check If A Set Is A Proper Subset.
- We can check if a set is a proper subset of another set using the
<
operator.
Example-1
print({1,2,3} < {1,2,3,4})
print({1,2,3} < {1,2,3})
True
False
(10) Add An Element To A Set.
- Unlike lists, we can’t use the
+
operator to add elements to a set. - Use the
add
method to add elements.
Example-1
{1,2,3} + {4}
TypeError: unsupported operand type(s) for +: 'set' and 'set'
Example-2
s = {'a','b','c'}
s.add('d')
print(s)
{'c', 'b', 'd', 'a'}
(11) Make A Copy Of A Set.
- The
copy()
method makes a shallow copy of a set.
Example-1
s1 = {'a','b','c'}
s2 = s1.copy()
print(s2)
{'c', 'b', 'a'}
(12) Check If A Set Is A Superset Of Another Set.
A set,
s1
is a superset of another set,s2
, if all values ins2
can be found ins1
.You can check if a set is a superset of another set with the
issuperset()
method.
Example-1
a = {10,20,30}
b = {10,20}
print(a.issuperset(b))
print(b.issuperset(a))
True
False
(13) Check If A Set Is A Superset With Comparison Operators.
- In addition to
issuperset()
, we can check if a set is a superset using the>=
comparison operators.
Example-1
a = {10,20,30}
b = {10,20}
print(a >= b)
print(b >= a)
True
False
(14) Is A Set A Superset Of Itself?
- Because all the values in a set,
s1
, are ins1
, it is a superset of itself. Though it is not a proper superset.
Example-1
a = {10,20,30}
a.issuperset(a)
True
(15) Check If A Set Is A Proper Superset Of Another Set.
A set,
s1
is a proper superset of another set,s2
, if all the values ins2
are ins1
, ands1 != s2
.This can be checked with the
>
operator.
Example-1
a = {10,20,30}
b = {10,20}
c = {10,20}
print(a > b)
print(b > c)
True
False
(16) Convert A Set To A List.
- Calling the list constructor,
list()
, on a set converts a set to a list. But note that order is not guaranteed.
Example-1
a = {4,2,3}
b = list(a)
print(b)
[2, 3, 4]
(17) How Can You Iterate On Values In A Set?
- A set can be iterated over like any other iterator with a loop. But note again, the order is not guaranteed.
Example-1
s = {'a','b','c','d','e'}
for i in s:
print(i)
d
e
c
b
a
(18) Return The Length Of A Set.
- The number of elements in a set can be returned with the
len()
function.
Example-1
s = {'a','b','c','d','e'}
len(s)
5
(19) Find The Union Of 2 Sets.
- The union of 2 sets can be found by using the
union()
method. - It can also be found with the
|
operator.
Example-1
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
s1.union(s2)
{1, 2, 3, 4, 5, 6, 7, 8}
Example-2
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
s1 | s2
{1, 2, 3, 4, 5, 6, 7, 8}
(20) Find The Intersection Of 2 Sets.
- The intersection of 2 sets can be taken with the intersection() method.
- It can also be taken with the
&
operator.
Example-1
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
s1.intersection(s2)
{4, 5}
Example-2
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
s1 & s2
{4, 5}
(21) Find The Elements In s1 That Are Not In s2.
- This can be found with the
difference()
method. - It can also be found with the
-
operator.
Example-1
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
s1.difference(s2)
{1, 2, 3}
Example-2
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
s1 - s2
{1, 2, 3}
(22) Remove An Element From A Set.
remove()
removes an element from a set by value.
Example-1
s = {'x','y','z'}
s.remove('x')
print(s)
{'y', 'z'}
(23) Remove And Return An Unspecified Element From A Set.
pop()
removes and returns an element from a set, treating the set like an unordered queue.
Example-1
s = {'z','y','x'}
removed = s.pop()
print(s)
print(removed)
{'y', 'z'}
x
(24) Check If 2 Sets Are Disjoint.
- Sets, s1 and s2 are disjoint if they have no elements in common.
Example-1
a = {1,2,3}
b = {4,5,6}
c = {3}
print(a.isdisjoint(b))
print(a.isdisjoint(c))
True
False
(25) Add All Elements From Another Set To An Existing Set.
- The
update()
method adds elements from another set. - This can also be done with the
|=
operator.
Example-1
a = {1,2,3}
b = {3,4,5}
a |= b
print(a)
{1, 2, 3, 4, 5}
(26) Remove All Elements From A Set
clear()
removes all elements from a set.- The set can then be used for future operations and store other values.
Example-1
a = {1,2,3}
a.clear()
print(a)
set()
(27) Remove An Element From A Set If It Exists
discard()
removes an element if that element exists, otherwise it does nothing.
Example-1
a = {1,2,3}
a.discard(1)
print(a)
{2, 3}
Example-2
a = {1,2,3}
a.discard(5)
print(a)
{1, 2, 3}
(28) What Is The Result Of Passing A Dictionary To A Set Constructor?
- Only the dictionary’s keys will exist in the returned set.
Example-1
d = {'dog': 1, 'cat':2, 'fish':3}
set(d)
{'cat', 'dog', 'fish'}
(29) Can You Zip 2 Sets Together?
Yes.
But the values from each set may not be joined in order.
Notice how the 1st value in the integer set was combined with the 3rd value in the letter set,
(1, 'c')
.
Example-1
set1 = {1,2,3}
set2 = {'a','b','c'}
z = zip(set1,set2)
set(z)
{(1, 'c'), (2, 'b'), (3, 'a')}
(30) Can A Set Be Accessed By Index?
- No.
- Trying to access a set by index will throw an error.
Example-1
s = {1,2,3}
s[0]
TypeError: 'set' object is not subscriptable
(31) What Is The Difference Between A Set And A Tuple?
Tuples are immutable. Sets are mutable.
Values in a tuple can be accessed by index.
Values in a set can only be accessed by value.
Tuples have order. Sets have no order.
Sets implement set theory, so they have lots of interesting functionality like union, intersect, difference, etc.
(32) What Is the Difference Between A Set And A Frozenset?
- Frozensets behave just like sets except they are immutable.
Example-1: Normal Set
s = set([1,2,3])
s.add(4)
print(s)
{1, 2, 3, 4}
- We can add elements to the normal Set.
Example-2: Frozen Set
fs = frozenset([1,2,3])
fs.add(4)
print(fs)
AttributeError Traceback (most recent call last)
Input In [56], in <cell line: 3>()
1 fs = frozenset([1,2,3])
----> 3 fs.add(4)
5 print(fs)
AttributeError: 'frozenset' object has no attribute 'add'
- We can not add elements to the frozen Set.