.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/basics/sets.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_basics_sets.py: ========== 1.7 sets ========== .. important:: This lesson is still under development. .. GENERATED FROM PYTHON SOURCE LINES 11-17 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. .. GENERATED FROM PYTHON SOURCE LINES 19-26 .. code-block:: default urdu = set("National language of Pakistan") print(type(urdu)) print(urdu) .. rst-class:: sphx-glr-script-out .. code-block:: none {'i', 'a', ' ', 'n', 'l', 'f', 'g', 'u', 'e', 't', 'k', 's', 'o', 'P', 'N'} .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: default pak_langs = set(["Balochi", "Barohi", "Sindhi", "Balti"]) print(pak_langs) .. rst-class:: sphx-glr-script-out .. code-block:: none {'Balochi', 'Barohi', 'Sindhi', 'Balti'} .. GENERATED FROM PYTHON SOURCE LINES 32-34 If our sequence contains repeating objects, only one instance of those repeating objects will be included in the list. .. GENERATED FROM PYTHON SOURCE LINES 36-40 .. code-block:: default pak_langs = set(("Balochi", "Barohi", "Sindhi", "Balti", "Balochi")) print(pak_langs) .. rst-class:: sphx-glr-script-out .. code-block:: none {'Balochi', 'Barohi', 'Sindhi', 'Balti'} .. GENERATED FROM PYTHON SOURCE LINES 41-42 Although, we can create sets from lists, but a set can not contain a list as an object. .. GENERATED FROM PYTHON SOURCE LINES 44-48 .. code-block:: default pak_langs = set((("Balochi", "Barohi"), ("punbabi", "siraiki"))) print(pak_langs) .. rst-class:: sphx-glr-script-out .. code-block:: none {('punbabi', 'siraiki'), ('Balochi', 'Barohi')} .. GENERATED FROM PYTHON SOURCE LINES 49-54 .. code-block:: default # uncomment following line # pak_langs = set((["Balochi", "Barohi"], ["punbabi", "siraiki"])) print(pak_langs) .. rst-class:: sphx-glr-script-out .. code-block:: none {('punbabi', 'siraiki'), ('Balochi', 'Barohi')} .. GENERATED FROM PYTHON SOURCE LINES 55-57 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 .. GENERATED FROM PYTHON SOURCE LINES 59-64 .. code-block:: default pak_langs = set(["Balochi", "Barohi", "Sindhi"]) pak_langs.add("Pashto") print(pak_langs) .. rst-class:: sphx-glr-script-out .. code-block:: none {'Balochi', 'Barohi', 'Sindhi', 'Pashto'} .. GENERATED FROM PYTHON SOURCE LINES 65-66 There are immutable sets as well with the name `frozenset`. .. GENERATED FROM PYTHON SOURCE LINES 68-74 .. code-block:: default balochistan_langs = frozenset(["Balochi", "Barohi", "Pashto"]) # uncomment following line # balochistan_langs.add("punjabi") .. GENERATED FROM PYTHON SOURCE LINES 75-80 .. code-block:: default # Operations on sets .. GENERATED FROM PYTHON SOURCE LINES 81-85 adding elements ----------------- We saw, how to add objects in sets with the method `add`. We can not violate aforementioned rules using `add` method. .. GENERATED FROM PYTHON SOURCE LINES 87-93 .. code-block:: default imperialists = {"bbc", "cnn"} # uncomment following line # imperialists.add(["voa","dw"]) # TypeError .. GENERATED FROM PYTHON SOURCE LINES 94-98 .. code-block:: default imperialists.add('bbc') print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none {'bbc', 'cnn'} .. GENERATED FROM PYTHON SOURCE LINES 99-103 .. code-block:: default imperialists.update(["voa","dw"]) print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none {'bbc', 'voa', 'cnn', 'dw'} .. GENERATED FROM PYTHON SOURCE LINES 104-111 .. code-block:: default imperialists = {"bbc", "cnn"} # uncomment following line # imperialists.update([["voa","dw"]]) # TypeError print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none {'bbc', 'cnn'} .. GENERATED FROM PYTHON SOURCE LINES 112-113 ``|`` operator can also be used to add/concatenate two sets .. GENERATED FROM PYTHON SOURCE LINES 115-120 .. code-block:: default imperialists = {"bbc", "cnn"} imperialists | {"voa", "dw"} .. rst-class:: sphx-glr-script-out .. code-block:: none {'bbc', 'voa', 'cnn', 'dw'} .. GENERATED FROM PYTHON SOURCE LINES 121-128 .. code-block:: default imperialists = {"bbc", "cnn"} imperialists |= {"voa", "dw"} print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none {'bbc', 'voa', 'cnn', 'dw'} .. GENERATED FROM PYTHON SOURCE LINES 129-132 `clear` ---------- We can clear the contents of a set by using the method `clear` on a set. .. GENERATED FROM PYTHON SOURCE LINES 134-140 .. code-block:: default dakus = {"musharaf", "nawaz", "benazir"} dakus.clear() # after NRO (https://en.wikipedia.org/wiki/National_Reconciliation_Ordinance) print(dakus) .. rst-class:: sphx-glr-script-out .. code-block:: none set() .. GENERATED FROM PYTHON SOURCE LINES 141-144 Copy ------ The assignment operation ``=`` does not create a new set. .. GENERATED FROM PYTHON SOURCE LINES 146-152 .. code-block:: default more_dakus = {"pervaiz elahi", "altaf husain"} dakus_backup = more_dakus more_dakus.clear() print(dakus_backup) .. rst-class:: sphx-glr-script-out .. code-block:: none set() .. GENERATED FROM PYTHON SOURCE LINES 153-154 ``copy`` method creates a shallow copy .. GENERATED FROM PYTHON SOURCE LINES 156-162 .. code-block:: default more_dakus = {"pervaiz elahi", "altaf husain"} dakus_backup = more_dakus.copy() more_dakus.clear() print(dakus_backup) .. rst-class:: sphx-glr-script-out .. code-block:: none {'altaf husain', 'pervaiz elahi'} .. GENERATED FROM PYTHON SOURCE LINES 163-174 .. code-block:: default imperialists = {"BBC", "CNN", "VOA"} more_imperialists = imperialists.copy() more_imperialists.add("DW") print(imperialists) print(more_imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none {'CNN', 'BBC', 'VOA'} {'CNN', 'DW', 'BBC', 'VOA'} .. GENERATED FROM PYTHON SOURCE LINES 175-177 ``difference`` ---------------- .. GENERATED FROM PYTHON SOURCE LINES 179-187 .. code-block:: default pml_q = {"zafrullah jamali", "fawad hussain", "pervaiz elahi", "umar ayyub"} pml_n = {"choi Nisar" , "umar ayyub", "khawaja Asif"} pti = {"firdows ashiq", "umar ayyub", "asad umar", "fawad hussain"} pml_q.difference(pti) .. rst-class:: sphx-glr-script-out .. code-block:: none {'pervaiz elahi', 'zafrullah jamali'} .. GENERATED FROM PYTHON SOURCE LINES 188-192 .. code-block:: default lotas_2013 = pml_q.difference(pml_q.difference(pml_n)) print(lotas_2013) .. rst-class:: sphx-glr-script-out .. code-block:: none {'umar ayyub'} .. GENERATED FROM PYTHON SOURCE LINES 193-194 We can also make use of ``-`` operator .. GENERATED FROM PYTHON SOURCE LINES 196-199 .. code-block:: default print(pml_q - pti) .. rst-class:: sphx-glr-script-out .. code-block:: none {'pervaiz elahi', 'zafrullah jamali'} .. GENERATED FROM PYTHON SOURCE LINES 200-204 `difference_update` ----------------------- This makes change in original set. similar to `x-y` with the exception that `x` is itself changed. .. GENERATED FROM PYTHON SOURCE LINES 206-215 .. code-block:: default pml_q = {"zafrullah jamali", "fawad hussain", "pervaiz elahi", "umar ayyub"} pml_n = {"choi Nisar" , "umar ayyub", "khawaja Asif"} pti = {"firdows ashiq", "umar ayyub", "asad umar", "fawad hussain"} pml_q.difference_update(pml_n) print(pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none {'zafrullah jamali', 'fawad hussain', 'pervaiz elahi'} .. GENERATED FROM PYTHON SOURCE LINES 216-221 .. code-block:: default pml_q.difference_update(pti) print(pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none {'pervaiz elahi', 'zafrullah jamali'} .. GENERATED FROM PYTHON SOURCE LINES 222-225 ``discard`` --------------- removes an element from set if it is present. .. GENERATED FROM PYTHON SOURCE LINES 227-232 .. code-block:: default pml_q = {"zafrullah jamali", "fawad hussain", "pervaiz elahi", "umar ayyub"} pml_q.discard("zafrullah jamali") print(pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none {'fawad hussain', 'umar ayyub', 'pervaiz elahi'} .. GENERATED FROM PYTHON SOURCE LINES 233-237 .. code-block:: default pml_q.discard("choi nisar") print(pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none {'fawad hussain', 'umar ayyub', 'pervaiz elahi'} .. GENERATED FROM PYTHON SOURCE LINES 238-240 `ferdows ashiq` is not present in set `musharaf` but using `discard` did not raise an error. .. GENERATED FROM PYTHON SOURCE LINES 240-245 .. code-block:: default ## ``remove`` # Same as `discard` with the exception that an error is raised if the object is # not present in set. .. GENERATED FROM PYTHON SOURCE LINES 246-251 .. code-block:: default pml_q = {"zafrullah jamali", "fawad hussain", "pervaiz elahi", "umar ayyub"} pml_q.remove("zafrullah jamali") print(pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none {'fawad hussain', 'umar ayyub', 'pervaiz elahi'} .. GENERATED FROM PYTHON SOURCE LINES 252-257 .. code-block:: default # uncomment following line # pml_q.remove("choi nisar") # KeyError print(pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none {'fawad hussain', 'umar ayyub', 'pervaiz elahi'} .. GENERATED FROM PYTHON SOURCE LINES 258-260 ``pop`` -------- .. GENERATED FROM PYTHON SOURCE LINES 262-267 .. code-block:: default pml_q = {"firdows ashiq", "fawad hussain", "pervaiz elahi", "umar ayyub"} pml_q.pop() print(pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none {'umar ayyub', 'firdows ashiq', 'pervaiz elahi'} .. GENERATED FROM PYTHON SOURCE LINES 268-272 .. code-block:: default pml_q.pop() print(pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none {'firdows ashiq', 'pervaiz elahi'} .. GENERATED FROM PYTHON SOURCE LINES 273-275 Running the above cell multiple times will eventually raise an error when the set becomes empty. .. GENERATED FROM PYTHON SOURCE LINES 277-279 `union` ------------- .. GENERATED FROM PYTHON SOURCE LINES 281-287 .. code-block:: default pml_q = {"firdows ashiq", "fawad hussain", "pervaiz elahi", "umar ayyub"} pml_n = {"choi Nisar" , "umar ayyub", "khawaja Asif"} pml_q.union(pml_n) .. rst-class:: sphx-glr-script-out .. code-block:: none {'khawaja Asif', 'fawad hussain', 'umar ayyub', 'firdows ashiq', 'choi Nisar', 'pervaiz elahi'} .. GENERATED FROM PYTHON SOURCE LINES 288-291 .. code-block:: default print(pml_q | pml_n) .. rst-class:: sphx-glr-script-out .. code-block:: none {'khawaja Asif', 'fawad hussain', 'umar ayyub', 'firdows ashiq', 'choi Nisar', 'pervaiz elahi'} .. GENERATED FROM PYTHON SOURCE LINES 292-295 .. code-block:: default ## `intersection` .. GENERATED FROM PYTHON SOURCE LINES 296-302 .. code-block:: default pml_q = {"firdows ashiq", "fawad hussain", "pervaiz elahi", "umar ayyub"} pti = {"firdows ashiq", "umar ayyub", "asad umar", "fawad hussain"} pml_q.intersection(pti) .. rst-class:: sphx-glr-script-out .. code-block:: none {'fawad hussain', 'umar ayyub', 'firdows ashiq'} .. GENERATED FROM PYTHON SOURCE LINES 303-304 We can also use ``&`` operator .. GENERATED FROM PYTHON SOURCE LINES 306-309 .. code-block:: default print(pml_q & pti) .. rst-class:: sphx-glr-script-out .. code-block:: none {'fawad hussain', 'umar ayyub', 'firdows ashiq'} .. GENERATED FROM PYTHON SOURCE LINES 310-311 The original set `pml_q` remains unchanged. .. GENERATED FROM PYTHON SOURCE LINES 313-316 .. code-block:: default print(pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none {'fawad hussain', 'umar ayyub', 'firdows ashiq', 'pervaiz elahi'} .. GENERATED FROM PYTHON SOURCE LINES 317-318 However, if we use `intersection_update`, the original set is changed .. GENERATED FROM PYTHON SOURCE LINES 320-324 .. code-block:: default pml_q.intersection_update(pti) print(pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none {'fawad hussain', 'umar ayyub', 'firdows ashiq'} .. GENERATED FROM PYTHON SOURCE LINES 325-326 If we want to find out intersection between multiple sets, we can do it as following. .. GENERATED FROM PYTHON SOURCE LINES 328-336 .. code-block:: default pml_q = {"firdows ashiq", "fawad hussain", "pervaiz elahi", "umar ayyub"} pti = {"firdows ashiq", "umar ayyub", "asad umar", "fawad hussain"} pml_n = {"choi Nisar" , "umar ayyub", "khawaja Asif"} sets = [pml_q, pml_n, pti] set.intersection(*sets) .. rst-class:: sphx-glr-script-out .. code-block:: none {'umar ayyub'} .. GENERATED FROM PYTHON SOURCE LINES 337-338 or .. GENERATED FROM PYTHON SOURCE LINES 340-344 .. code-block:: default sets = [pml_n, pti] pml_q.intersection(*sets) .. rst-class:: sphx-glr-script-out .. code-block:: none {'umar ayyub'} .. GENERATED FROM PYTHON SOURCE LINES 345-347 So we can say that `umar ayyub `_ is the most consistent turncoat. .. GENERATED FROM PYTHON SOURCE LINES 349-352 `isdisjoint` -------------- returns `True` if the intersection of two sets is not empty set. .. GENERATED FROM PYTHON SOURCE LINES 354-361 .. code-block:: default ppp = {"firdows ashiq", "fawad hussain", "Amin Faheem", "umar ayyub"} pti = {"firdows ashiq", "umar ayyub", "asad umar", "fawad hussain"} ji = {"liaquat baloch", "siraj ul haq", "munawar hasan"} ppp.isdisjoint(ji) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 362-365 .. code-block:: default ppp.isdisjoint(pti) .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 366-371 .. code-block:: default # `issubset` # ------------ # ``<`` is used for proper subset and ``<=`` is used for subset checking. .. GENERATED FROM PYTHON SOURCE LINES 372-378 .. code-block:: default pml_n = {"nawaz", "shahbaz", "pervaiz elahi", "mushahid husain"} pml_q = {"pervaiz elahi", "mushahid husain"} pml_q.issubset(pml_n) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 379-382 .. code-block:: default print(pml_q <= pml_n) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 383-386 .. code-block:: default print(pml_q < pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 387-390 ``issuperset`` -------------- ``>`` is used for proper superset and ``>=`` is used for superset checking. .. GENERATED FROM PYTHON SOURCE LINES 392-398 .. code-block:: default pml_n = {"nawaz", "shahbaz", "pervaiz elahi", "mushahid husain"} pml_q = {"pervaiz elahi", "mushahid husain"} pml_n.issuperset(pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 399-402 .. code-block:: default print(pml_n >= pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 403-406 .. code-block:: default print(pml_n > pml_n) .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 407-411 .. code-block:: default # Since sets are unordered, the operation ``in`` is faster when applied to # sets as compared to lists. .. GENERATED FROM PYTHON SOURCE LINES 412-415 .. code-block:: default print("nawaz" in pml_n) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 416-418 .. code-block:: default print("nawaz" not in pml_q) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.015 seconds) .. _sphx_glr_download_auto_examples_basics_sets.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/basics/sets.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: sets.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: sets.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_