35 Questions To Test Your Knowledge of Python Sets | by GreekDataGuy | Towards Data Science

In my pursuit of interview algorithm mastery, I’ve found it useful to dig into Python’s basic data structures.
This is my personal list of questions I wrote to evaluate my knowledge of Python sets.
How well do you know python sets?
No. A set is not an ordered data structure, so order is not maintained.
Look what happens when we convert the list [3,2,1] to a set. It becomes {1,2,3}.
a = set([3,2,1])a #=> {1, 2, 3}
This can be done with the issubset() method.
a = {4,5}b = {1,2,3,4,5}a.issubset(b) #=> Trueb.issubset(a)#=> False
A set, s1, is a subset of s2 if all elements of s1 are in s2.
The operators <= will return True if all elements of the 1st set exist in the 2nd set (aka. is a subset).
a = {'a','b'}b = {'a','b','c'}a <= b#=> Trueb <= a #=> False
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.
a = {10,20}a.issubset(a) #=> True
Like other types of iterables, we can check if a value exists in a set with the in operator.
s = {5,7,9}5 in s#=> True6 in s#=> False
We can again use the in operator, but this time prefaced by not.
s = {'x','y''z'}'w' not in s#=> True'x' not in s#=> False
A set is an unordered collection of unique objects.
Duplicate values are removed when any object is converted to a set.
set([1,1,1,3,5])#=> {1, 3, 5}
In my personal experience, I use sets because they make operations like finding intersections, unions and differences easy.
A proper subset is a subset of a set, not equal to itself.
{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.
We can check if a set is a proper subset of another set using the < operator.
{1,2,3} < {1,2,3,4}#=> True{1,2,3} < {1,2,3}#=> False
Unlike lists, we can’t use the + operator to add elements to a set.
{1,2,3} + {4}#=> TypeError: unsupported operand type(s) for +: 'set' and 'set'
Use the add method to add elements.
s = {'a','b','c'}s.add('d')s#=> {'a', 'b', 'c', 'd'}
The copy() method makes a shallow copy of a set.
s1 = {'a','b','c'}s2 = s1.copy()s2 #=> {'c', 'b', 'a'}
A set, s1 is a superset of another set, s2, if all values in s2 can be found in s1.
You can check if a set is a superset of another set with the issuperset() method.
a = {10,20,30}b = {10,20}a.issuperset(b) #=> Trueb.issuperset(a) #=> False
In addition to issuperset(), we can check if a set is a superset using the >= comparison operators.
a = {10,20,30}b = {10,20}a >= b #=> Trueb >= a #=> False
Because all the values in a set, s1, are in s1, it is a superset of itself. Though it is not a proper superset.
a = {10,20,30}a.issuperset(a) #=> True
A set, s1 is a proper superset of another set, s2, if all the values in s2 are in s1, and s1 != s2.
This can be checked with the > operator.
a = {10,20,30}b = {10,20}c = {10,20}a > b #=> Trueb > c #=> False
Calling the list constructor, list(), on a set converts a set to a list. But note that order is not guaranteed.
a = {4,2,3}list(a)#=> [2, 3, 4]
A set can be iterated over like any other iterator with a loop. But note again, order is not guaranteed.
s = {'a','b','c','d','e'}for i in s:    print(i)    #=> b#=> c#=> e#=> d#=> a
The number of elements in a set can be returned with the len() function.
s = {'a','b','c','d','e'}len(s)#=> 5
A set can be created using set notation with curly braces like, {...}, example:
{1,2,3}#=> {1, 2, 3}
Or with the set constructor, example:
# set(1,2,3)=> TypeError: set expected at most 1 arguments, got 3set([1,2,3])#=> {1, 2, 3}
But note the latter requires passing in another iterable object like a list.
The union of 2 sets can be found by using the union() method.
s1 = {1,2,3,4,5}s2 = {4,5,6,7,8}s1.union(s2)#=> {1, 2, 3, 4, 5, 6, 7, 8}
It can also be found with the | operator.
s1 = {1,2,3,4,5}s2 = {4,5,6,7,8}s1 | s2#=> {1, 2, 3, 4, 5, 6, 7, 8}
The intersection of 2 sets can be taken with the intersection() method.
s1 = {1,2,3,4,5}s2 = {4,5,6,7,8}s1.intersection(s2)# => {4, 5}
It can also be taken with the & operator.
s1 = {1,2,3,4,5}s2 = {4,5,6,7,8}s1 & s2# => {4, 5}
This can be found with the difference() method.
s1 = {1,2,3,4,5}s2 = {4,5,6,7,8}s1.difference(s2){1, 2, 3}
It can also be found with the - operator.
s1 = {1,2,3,4,5}s2 = {4,5,6,7,8}s1 - s2{1, 2, 3}
remove() removes an element from a set by value.
s = {'x','y','z'}s.remove('x')s#=> {'y', 'z'}
pop() removes and returns an element from a set, treating the set like an unordered queue.
s = {'z','y','x'}s.pop()s#=> {'y', 'z'}
Sets, s1 and s2 are disjoint if they have no elements in common.
a = {1,2,3}b = {4,5,6}c = {3}a.isdisjoint(b) #=> Truea.isdisjoint(c) #=> False
The update() method adds elements from another set.
a = {1,2,3}b = {3,4,5}a.update(b)a#=> {1, 2, 3, 4, 5}
This can also be done with the |= operator.
a = {1,2,3}b = {3,4,5}a |= ba#=> {1, 2, 3, 4, 5}
Note this is different from union. union() returns a new set instead of updating an existing set.
clear() removes all elements from a set. The set can then be used for future operations and store other values.
a = {1,2,3}a.clear()a#=> set()
discard() removes an element if that element exists, otherwise it does nothing.
a = {1,2,3}a.discard(1)a#=> {2, 3}a = {1,2,3}a.discard(5)a#=> {1, 2, 3}
Contrast this to remove() which throws an error if you try to remove an element that doesn’t exist.
a = {1,2,3}a.remove(5)a#=> KeyError: 5
Only the dictionary’s keys will exist in the returned set.
d = {'dog': 1, 'cat':2, 'fish':3}set(d)#=> {'cat', 'dog', 'fish'}
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').
z = zip(  {1,2,3},  {'a','b','c'})list(z)#=> [(1, 'c'), (2, 'b'), (3, 'a')]
No. Trying to access a set by index will throw an error.
s = {1,2,3}s[0]#=> TypeError: 'set' object is not subscriptable
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.
Frozensets behave just like sets except they are immutable.
s = set([1,2,3])fs = frozenset([1,2,3])s #=> {1, 2, 3}fs #=> frozenset({1, 2, 3})s.add(4)s #=> {1, 2, 3, 4}fs.add(4)fs #=> AttributeError: 'frozenset' object has no attribute 'add'
intersection_update() updates the first set to be equal to the intersection.
s1 = {1,2,3,4,5}s2 = {4,5,6,7,8}s1.intersection_update(s2)s1#=> {4, 5}
This can also be done with the &= operator.
s1 = {1,2,3,4,5}s2 = {4,5,6,7,8}s1 &= s2s1#=> {4, 5}
difference_update() removes the intersection from the first set.
s1 = {1,2,3,4,5}s2 = {4,5,6,7,8}s1.difference_update(s2)s1#=> {1, 2, 3}
The operator -= also works.
s1 = {1,2,3,4,5}s2 = {4,5,6,7,8}s1 -= s2s1#=> {1, 2, 3}
How did you do?
Honestly, I did worse here than on my string questions and list questions.
We don’t often use sets in day to day programming, and most of us only touch set theory when writing SQL joins. That said it doesn’t hurt to be familiar with the basics.
If you found this interesting you might also like my python interview questions.
Onward and upward.