sets
.
This tutorial will be much easy and funny to you if you liked mathematics in your high schools.
Huh! wait a minute... Didn't we learn something about sets in our getting started?, hahaha, obviously we didn't.
That was just the getting started, and now is the advanced level for us to learn everything we need to know in Python.
Let's dismantle the python set
piece-by-piece.
What did the teacher say at the high school about sets?
Wasn't it that a Set
is a collection of distinct objects, considered as an object in its own. right? and in fact, that is the real meaning of sets in python.
In fact, the Set
in python is the unordered collection of unique elements.
If you read correctly in the above definition, a set has only one entry of its items, ie no duplicates.
As a result of being Unordered, the python sets do not support Indexing
, and just like
tuples
, the Set is immutable.
A set data is within the oppening and close brackets { }
and data within it are seperated by commas.
Enough has been talked about the python sets, let's now see how we can thwart against the python sets.
>>> set1 = set()
add
All the items looks like that in lists and are seperated by the commas as in below example:>>> set1 = {1,2,3}
sum, min, max
etc as we did in previous tutorials.>>> set1 = [1,2,3]
>>> sum(set1) # 6
sum()
function takes an iterable object containing numbers and returns the addition of all of them.>>> min(set1) # 1
min()
function returns the minimum/the smallest of all the items in the set.>>> max(set1) # 3
max()
function is the opposite of the min()
function, and returns the greatest/biggest item in the set.
.add()
add()
inserts the item into the set if the item is not available,
and if the item is already in the set, then this method does nothing.
Here is how we can achieve this:>>> set1 = {1}
>>> set1.add(2)
>>> print(set1) # {1, 2}
.clear()
clear()
in lists, it removes all the elements of the concerned set.clear()
function.>>> set1 = {1}
>>> set1.clear()
>>> print(set1) # set()
.copy()
copy()
function to store the replica of our data.>>> set1 = {1}
>>> set2 = set1.copy()
>>> print(set2) # {1}
.difference(set, [others])
>>> set1 = {1, 5, 3}
>>> set2 = set1.difference({1, 2, 3})
>>> print(set2) # {5, 3}
.difference_update(set, [others])
>>> set1 = {1, 5, 3}
>>> set1.difference_update({1, 2, 3})
>>> print(set1) # {5}
.discard(item)
>>> set1 = {1, 5, 3}
>>> set1.discard(3)
>>> print(set1) # {1, 5}
.intersection(set)
>>> set1 = {1, 5, 3}
>>> set2 = set1.intersection({1,2,3})
>>> print(set2) # {1}
.intersection_update(set)
>>> set1 = {1, 5, 3}
>>> set1.intersection_update({1,2,3})
>>> print(set1) # {1}
.isdisjoint(set)
True
if the two sets are have no any intersection and returns False
otherwise.
>>> set1 = {1, 5,3}
>>> set2 = set1.isdisjoint({1,2,3})
>>> print(set2) # False
.issubset(set)
True
is the concerned set is the subset of a passed set, and returns False otherwise(ie if the set is a superset).
>>> set1 = {1, 5, 3}
>>> set2 = set1.issubset({1,2,3})
>>> print(set2) # False
>>> print(set1.issubset({1,7,5,3,9}) True
.issupperset(set)
True
is the concerned set is the supperset of a parsed set, and returns False otherwise(ie if the set is a subset).
>>> set1 = {1, 5, 3}
>>> set2 = set1.issupperset({1,2,3})
>>> print(set2) # False
>>> print(set1.issupperset({1,3}) True
.pop()
KeyError
.
>>> set1 = {1, 5, 3}
>>> set2 = set1.pop()
>>> print(set2) # 1
.pop()
KeyError
.
>>> set1 = {1, 5, 3}
>>> set1.remove(5)
>>> print(set2) # {1, 3}
.symmetric_difference(set)
>>> set1 = {1, 5, 3}
>>> set2 = set1.symmetric_difference({2,3,4})
>>> print(set2) # {1, 5,2,4}
.symmetric_difference_update(set)
>>> set1 = {1, 5, 3}
>>> set1.symmetric_difference_update({2,3,4})
>>> print(set1) # {1, 5,2,4}
.union(set)
>>> set1 = {1, 5, 3}
>>> set2 = set1.union({2,3,4})
>>> print(set2) # {1,2,3,4,5}
.update(set)
>>> set1 = {1, 5, 3}
>>> set1.update({2,3,4})
>>> print(set2) # {1, 5,3,2,4}