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'>
{' ', 'k', 'l', 's', 'i', 'n', 'P', 'a', 't', 'u', 'f', 'e', 'o', 'g', 'N'}
{'Sindhi', 'Barohi', 'Balochi', 'Balti'}
If our sequence contains repeating objects, only one instance of those repeating objects will be included in the list.
{'Sindhi', 'Barohi', 'Balochi', '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
{'Sindhi', 'Barohi', 'Balochi', '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)
{'cnn', 'bbc'}
imperialists.update(["voa","dw"])
print(imperialists)
{'voa', 'dw', 'cnn', 'bbc'}
imperialists = {"bbc", "cnn"}
# uncomment following line
# imperialists.update([["voa","dw"]]) # TypeError
print(imperialists)
{'cnn', 'bbc'}
| operator can also be used to add/concatenate two sets
imperialists = {"bbc", "cnn"}
imperialists | {"voa", "dw"}
{'voa', 'dw', 'cnn', 'bbc'}
imperialists = {"bbc", "cnn"}
imperialists |= {"voa", "dw"}
print(imperialists)
{'voa', 'dw', 'cnn', 'bbc'}
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)
{'pervaiz elahi', 'altaf husain'}
imperialists = {"BBC", "CNN", "VOA"}
more_imperialists = imperialists.copy()
more_imperialists.add("DW")
print(imperialists)
print(more_imperialists)
{'CNN', 'BBC', 'VOA'}
{'CNN', 'BBC', 'VOA', 'DW'}
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.
{'pervaiz elahi', 'zafrullah jamali', 'fawad hussain'}
{'pervaiz elahi', 'zafrullah jamali'}
discard#
removes an element from set if it is present.
{'pervaiz elahi', 'umar ayyub', 'fawad hussain'}
{'pervaiz elahi', 'umar ayyub', 'fawad hussain'}
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.
{'pervaiz elahi', 'umar ayyub', 'fawad hussain'}
# uncomment following line
# pml_q.remove("choi nisar") # KeyError
print(pml_q)
{'pervaiz elahi', 'umar ayyub', 'fawad hussain'}
pop#
{'umar ayyub', 'fawad hussain', 'firdows ashiq'}
{'fawad hussain', 'firdows ashiq'}
Running the above cell multiple times will eventually raise an error when the set becomes empty.
union#
{'pervaiz elahi', 'choi Nisar', 'khawaja Asif', 'umar ayyub', 'fawad hussain', 'firdows ashiq'}
{'pervaiz elahi', 'choi Nisar', 'khawaja Asif', 'umar ayyub', 'fawad hussain', 'firdows ashiq'}
## `intersection`
{'umar ayyub', 'fawad hussain', 'firdows ashiq'}
We can also use & operator
{'umar ayyub', 'fawad hussain', 'firdows ashiq'}
The original set pml_q remains unchanged.
print(pml_q)
{'pervaiz elahi', 'umar ayyub', 'fawad hussain', 'firdows ashiq'}
However, if we use intersection_update, the original set is changed
{'umar ayyub', 'fawad hussain', '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.016 seconds)