2.5 collections#

Important

This lesson is still under development.

books = {"AlSadr": ["Our Philosophy", "Our Economy"],
         "Mutahri": ["Divine Justice", "Man and Destiny"]}

defaultdict#

In a normal dictionary, when we try to access value of a non-existent key, we will get KeyError.

man = {'name': 'Ali'}

print(man['name'])
Ali
# uncomment following line
# man['height'] # KeyError

If however, we want to avoid this error, we can make use of defaultdict function. The input to defaultdict must be a callable.

from collections import defaultdict

print(int())
0
man = defaultdict(int)
man['weight'] = 75
print(man['height'])
0

deque#

from collections import  deque
a = deque([1,2,3], maxlen=5)

print(a)
deque([1, 2, 3], maxlen=5)
a.append(4)
print(a)

a.append(5)
print(a)

a.append(6)
print(a)
deque([1, 2, 3, 4], maxlen=5)
deque([1, 2, 3, 4, 5], maxlen=5)
deque([2, 3, 4, 5, 6], maxlen=5)
a.appendleft(1)
print(a)
deque([1, 2, 3, 4, 5], maxlen=5)
my_deque = deque([1, 2, 3], maxlen=3)

my_deque.append(4)

print(my_deque)
deque([2, 3, 4], maxlen=3)

namedtuple#

from collections import  namedtuple

OrderedDict#

from collections import  OrderedDict

Counter#

from collections import  Counter

KeysView#

It is used to check whether an object is the keys of a dictionary or not.

try:
    from collections import KeysView
except ImportError:  # python>3.9
    from collections.abc import KeysView
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/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

if we wish to check that whether a python object is key or value, we can achieve this as following

isinstance(books.keys(), KeysView)  # -> True
True

ValuesView#

It is used to check whether an object is the values of a dictionary or not.

try:
    from collections import ValuesView
except ImportError:  # python>3.9
    from collections.abc import ValuesView
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/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

similarly for dictionary values

isinstance(books.values(), ValuesView)  # -> True
True

Reversible

try:
    from collections import Reversible
except ImportError:  # python>3.9
    from collections.abc import Reversible
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/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

Set#

try:
    from collections import Set
except ImportError:  # python>3.9
    from collections.abc import Set
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/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

Sequence#

try:
    from collections import Sequence
except ImportError:  # python>3.9
    from collections.abc import Sequence
/home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/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

Total running time of the script: ( 0 minutes 0.006 seconds)

Gallery generated by Sphinx-Gallery