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