.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/builtin_modules/_collections.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_builtin_modules__collections.py: ================ 2.5 collections ================ .. important:: This lesson is still under development. .. GENERATED FROM PYTHON SOURCE LINES 8-12 .. code-block:: default books = {"AlSadr": ["Our Philosophy", "Our Economy"], "Mutahri": ["Divine Justice", "Man and Destiny"]} .. GENERATED FROM PYTHON SOURCE LINES 13-17 defaultdict -------------- In a normal dictionary, when we try to access value of a non-existent key, we will get ``KeyError``. .. GENERATED FROM PYTHON SOURCE LINES 17-20 .. code-block:: default man = {'name': 'Ali'} print(man['name']) .. rst-class:: sphx-glr-script-out .. code-block:: none Ali .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: default # uncomment following line # man['height'] # KeyError .. GENERATED FROM PYTHON SOURCE LINES 26-28 If however, we want to avoid this error, we can make use of ``defaultdict`` function. The input to ``defaultdict`` must be a callable. .. GENERATED FROM PYTHON SOURCE LINES 28-33 .. code-block:: default from collections import defaultdict print(int()) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 .. GENERATED FROM PYTHON SOURCE LINES 34-39 .. code-block:: default man = defaultdict(int) man['weight'] = 75 print(man['height']) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 .. GENERATED FROM PYTHON SOURCE LINES 40-42 deque -------------- .. GENERATED FROM PYTHON SOURCE LINES 42-47 .. code-block:: default from collections import deque a = deque([1,2,3], maxlen=5) print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none deque([1, 2, 3], maxlen=5) .. GENERATED FROM PYTHON SOURCE LINES 48-57 .. code-block:: default a.append(4) print(a) a.append(5) print(a) a.append(6) print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none deque([1, 2, 3, 4], maxlen=5) deque([1, 2, 3, 4, 5], maxlen=5) deque([2, 3, 4, 5, 6], maxlen=5) .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: default a.appendleft(1) print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none deque([1, 2, 3, 4, 5], maxlen=5) .. GENERATED FROM PYTHON SOURCE LINES 63-69 .. code-block:: default my_deque = deque([1, 2, 3], maxlen=3) my_deque.append(4) print(my_deque) .. rst-class:: sphx-glr-script-out .. code-block:: none deque([2, 3, 4], maxlen=3) .. GENERATED FROM PYTHON SOURCE LINES 70-72 namedtuple -------------- .. GENERATED FROM PYTHON SOURCE LINES 72-75 .. code-block:: default from collections import namedtuple .. GENERATED FROM PYTHON SOURCE LINES 76-78 OrderedDict -------------- .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: default from collections import OrderedDict .. GENERATED FROM PYTHON SOURCE LINES 81-83 Counter -------------- .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: default from collections import Counter .. GENERATED FROM PYTHON SOURCE LINES 86-89 KeysView --------- It is used to check whether an object is the keys of a dictionary or not. .. GENERATED FROM PYTHON SOURCE LINES 89-94 .. code-block:: default try: from collections import KeysView except ImportError: # python>3.9 from collections.abc import KeysView .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/scripts/builtin_modules/_collections.py:91: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working from collections import KeysView .. GENERATED FROM PYTHON SOURCE LINES 95-97 if we wish to check that whether a python object is key or value, we can achieve this as following .. GENERATED FROM PYTHON SOURCE LINES 97-101 .. code-block:: default isinstance(books.keys(), KeysView) # -> True .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 102-105 ValuesView ----------- It is used to check whether an object is the values of a dictionary or not. .. GENERATED FROM PYTHON SOURCE LINES 105-110 .. code-block:: default try: from collections import ValuesView except ImportError: # python>3.9 from collections.abc import ValuesView .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/scripts/builtin_modules/_collections.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working from collections import ValuesView .. GENERATED FROM PYTHON SOURCE LINES 111-112 similarly for dictionary values .. GENERATED FROM PYTHON SOURCE LINES 112-114 .. code-block:: default isinstance(books.values(), ValuesView) # -> True .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 115-116 Reversible .. GENERATED FROM PYTHON SOURCE LINES 116-121 .. code-block:: default try: from collections import Reversible except ImportError: # python>3.9 from collections.abc import Reversible .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/scripts/builtin_modules/_collections.py:117: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working from collections import Reversible .. GENERATED FROM PYTHON SOURCE LINES 122-124 Set --------- .. GENERATED FROM PYTHON SOURCE LINES 124-130 .. code-block:: default try: from collections import Set except ImportError: # python>3.9 from collections.abc import Set .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/scripts/builtin_modules/_collections.py:126: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working from collections import Set .. GENERATED FROM PYTHON SOURCE LINES 131-133 Sequence --------- .. GENERATED FROM PYTHON SOURCE LINES 133-138 .. code-block:: default try: from collections import Sequence except ImportError: # python>3.9 from collections.abc import Sequence .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/scripts/builtin_modules/_collections.py:135: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working from collections import Sequence .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.006 seconds) .. _sphx_glr_download_auto_examples_builtin_modules__collections.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/builtin_modules/_collections.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: _collections.py <_collections.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: _collections.ipynb <_collections.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_