1.7 sets
Contents
Note
Click here to download the full example code or to run this example in your browser via Binder
1.7 sets#
Important
This lesson is still under development.
A set is a collection of objects just like lists with the exception that it is unordered, does not contain same objects more than once, and can not contain immutable objects like lists.
A set can be created from an existing sequence object such as a string, list or tuple.
<class 'set'>
{'i', 'a', ' ', 'n', 'l', 'f', 'g', 'u', 'e', 't', 'k', 's', 'o', 'P', 'N'}
{'Balochi', 'Barohi', 'Sindhi', 'Balti'}
If our sequence contains repeating objects, only one instance of those repeating objects will be included in the list.
{'Balochi', 'Barohi', 'Sindhi', 'Balti'}
Although, we can create sets from lists, but a set can not contain a list as an object.
{('punbabi', 'siraiki'), ('Balochi', 'Barohi')}
# uncomment following line
# pak_langs = set((["Balochi", "Barohi"], ["punbabi", "siraiki"]))
print(pak_langs)
{('punbabi', 'siraiki'), ('Balochi', 'Barohi')}
In second case above, we want our set to have two lists as objects, so the error was prompted. Sets are mutable i.e. they can be changed. We can add new objects in sets as following
{'Balochi', 'Barohi', 'Sindhi', 'Pashto'}
There are immutable sets as well with the name frozenset.
balochistan_langs = frozenset(["Balochi", "Barohi", "Pashto"])
# uncomment following line
# balochistan_langs.add("punjabi")
# Operations on sets
adding elements#
We saw, how to add objects in sets with the method add. We can not violate aforementioned rules using add method.
imperialists = {"bbc", "cnn"}
# uncomment following line
# imperialists.add(["voa","dw"]) # TypeError
imperialists.add('bbc')
print(imperialists)
{'bbc', 'cnn'}
imperialists.update(["voa","dw"])
print(imperialists)
{'bbc', 'voa', 'cnn', 'dw'}
imperialists = {"bbc", "cnn"}
# uncomment following line
# imperialists.update([["voa","dw"]]) # TypeError
print(imperialists)
{'bbc', 'cnn'}
| operator can also be used to add/concatenate two sets
imperialists = {"bbc", "cnn"}
imperialists | {"voa", "dw"}
{'bbc', 'voa', 'cnn', 'dw'}
imperialists = {"bbc", "cnn"}
imperialists |= {"voa", "dw"}
print(imperialists)
{'bbc', 'voa', 'cnn', 'dw'}
clear#
We can clear the contents of a set by using the method clear on a set.
set()
Copy#
The assignment operation = does not create a new set.
more_dakus = {"pervaiz elahi", "altaf husain"}
dakus_backup = more_dakus
more_dakus.clear()
print(dakus_backup)
set()
copy method creates a shallow copy
more_dakus = {"pervaiz elahi", "altaf husain"}
dakus_backup = more_dakus.copy()
more_dakus.clear()
print(dakus_backup)
{'altaf husain', 'pervaiz elahi'}
imperialists = {"BBC", "CNN", "VOA"}
more_imperialists = imperialists.copy()
more_imperialists.add("DW")
print(imperialists)
print(more_imperialists)
{'CNN', 'BBC', 'VOA'}
{'CNN', 'DW', 'BBC', 'VOA'}
difference#
{'pervaiz elahi', 'zafrullah jamali'}
lotas_2013 = pml_q.difference(pml_q.difference(pml_n))
print(lotas_2013)
{'umar ayyub'}
We can also make use of - operator
{'pervaiz elahi', 'zafrullah jamali'}
difference_update#
This makes change in original set. similar to x-y with the exception that x is itself changed.
{'zafrullah jamali', 'fawad hussain', 'pervaiz elahi'}
{'pervaiz elahi', 'zafrullah jamali'}
discard#
removes an element from set if it is present.
{'fawad hussain', 'umar ayyub', 'pervaiz elahi'}
{'fawad hussain', 'umar ayyub', 'pervaiz elahi'}
ferdows ashiq is not present in set musharaf but using discard did not raise an error.
## ``remove``
# Same as `discard` with the exception that an error is raised if the object is
# not present in set.
{'fawad hussain', 'umar ayyub', 'pervaiz elahi'}
# uncomment following line
# pml_q.remove("choi nisar") # KeyError
print(pml_q)
{'fawad hussain', 'umar ayyub', 'pervaiz elahi'}
pop#
{'umar ayyub', 'firdows ashiq', 'pervaiz elahi'}
{'firdows ashiq', 'pervaiz elahi'}
Running the above cell multiple times will eventually raise an error when the set becomes empty.
union#
{'khawaja Asif', 'fawad hussain', 'umar ayyub', 'firdows ashiq', 'choi Nisar', 'pervaiz elahi'}
{'khawaja Asif', 'fawad hussain', 'umar ayyub', 'firdows ashiq', 'choi Nisar', 'pervaiz elahi'}
## `intersection`
{'fawad hussain', 'umar ayyub', 'firdows ashiq'}
We can also use & operator
{'fawad hussain', 'umar ayyub', 'firdows ashiq'}
The original set pml_q remains unchanged.
print(pml_q)
{'fawad hussain', 'umar ayyub', 'firdows ashiq', 'pervaiz elahi'}
However, if we use intersection_update, the original set is changed
{'fawad hussain', 'umar ayyub', 'firdows ashiq'}
If we want to find out intersection between multiple sets, we can do it as following.
{'umar ayyub'}
or
{'umar ayyub'}
So we can say that umar ayyub is the most consistent turncoat.
isdisjoint#
returns True if the intersection of two sets is not empty set.
True
False
# `issubset`
# ------------
# ``<`` is used for proper subset and ``<=`` is used for subset checking.
True
True
False
issuperset#
> is used for proper superset and >= is used for superset checking.
True
True
False
# Since sets are unordered, the operation ``in`` is faster when applied to
# sets as compared to lists.
print("nawaz" in pml_n)
True
print("nawaz" not in pml_q)
True
Total running time of the script: ( 0 minutes 0.015 seconds)