pydoc > collections

📛 NAME

collections

🚀 Quick Reference

Use CaseCommandDescription
🔢 Count hashable itemsCounter(iterable)Dict subclass for counting elements
📋 Ordered dictionaryOrderedDict()Dict that remembers insertion order
🔗 Multiple mappings as oneChainMap(*maps)Single view of multiple dicts
🏭 Default factory for missing keysdefaultdict(factory)Dict with default value on missing key
⚡ Fast appends/pops both endsdeque([iterable[, maxlen]])List-like with O(1) appends/pops on either end
📦 Named tuple with fieldsnamedtuple(typename, field_names)Tuple subclass with named fields
🛠️ Easy dict subclassingUserDict()Wrapper around dict for easier subclassing
🛠️ Easy list subclassingUserList()Wrapper around list for easier subclassing
🛠️ Easy string subclassingUserString(seq)Wrapper around string for easier subclassing

📖 MODULE REFERENCE

https://docs.python.org/3.10/library/collections.html

The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above.

📝 DESCRIPTION

This module implements specialized container datatypes providing alternatives to Python's general purpose built-in containers, dict, list, set, and tuple.

📦 PACKAGE CONTENTS

abc

📂 SUBMODULES

_collections_abc

📚 CLASSES

builtins.dict(builtins.object) → Counter, OrderedDict, defaultdict
builtins.object → deque
collections.abc.MutableMapping(collections.abc.Mapping) → ChainMap, UserDict
collections.abc.MutableSequence(collections.abc.Sequence) → UserList
collections.abc.Sequence(collections.abc.Reversible, collections.abc.Collection) → UserString

🔗 class ChainMap(collections.abc.MutableMapping)

ChainMap(*maps)

A ChainMap groups multiple dicts (or other mappings) together to create a single, updateable view.

The underlying mappings are stored in a list. That list is public and can be accessed or updated using the maps attribute. There is no other state.

Lookups search the underlying mappings successively until a key is found. In contrast, writes, updates, and deletions only operate on the first mapping.

Method resolution order: ChainMap, collections.abc.MutableMapping, collections.abc.Mapping, collections.abc.Collection, collections.abc.Sized, collections.abc.Iterable, collections.abc.Container, builtins.object

Methods defined here:

Class methods defined here:

Readonly properties defined here:

Data descriptors defined here:

Data and other attributes defined here:

Methods inherited from collections.abc.MutableMapping:

Methods inherited from collections.abc.Mapping:

Data and other attributes inherited from collections.abc.Mapping:

Class methods inherited from collections.abc.Collection:

Class methods inherited from collections.abc.Iterable:

🔢 class Counter(builtins.dict)

Counter(iterable=None, /, **kwds)

Dict subclass for counting hashable items. Sometimes called a bag or multiset. Elements are stored as dictionary keys and their counts are stored as dictionary values.

>>> c = Counter('abcdeabcdabcaba')  # count elements from a string

>>> c.most_common(3)                # three most common elements
[('a', 5), ('b', 4), ('c', 3)]
>>> sorted(c)                       # list all unique elements
['a', 'b', 'c', 'd', 'e']
>>> ''.join(sorted(c.elements()))   # list elements with repetitions
'aaaaabbbbcccdde'
>>> sum(c.values())                 # total of all counts
15

>>> c['a']                          # count of letter 'a'
5
>>> for elem in 'shazam':           # update counts from an iterable
...     c[elem] += 1                # by adding 1 to each element's count
>>> c['a']                          # now there are seven 'a'
7
>>> del c['b']                      # remove all 'b'
>>> c['b']                          # now there are zero 'b'
0

>>> d = Counter('simsalabim')       # make another counter
>>> c.update(d)                     # add in the second counter
>>> c['a']                          # now there are nine 'a'
9

>>> c.clear()                       # empty the counter
>>> c
Counter()

Note: If a count is set to zero or reduced to zero, it will remain in the counter until the entry is deleted or the counter is cleared:

>>> c = Counter('aaabbc')
>>> c['b'] -= 2                     # reduce the count of 'b' by two
>>> c.most_common()                 # 'b' is still in, but its count is zero
[('a', 3), ('c', 1), ('b', 0)]

Method resolution order: Counter, builtins.dict, builtins.object

Methods defined here:

Class methods defined here:

Data descriptors defined here:

Data and other attributes defined here:

Methods inherited from builtins.dict:

Class methods inherited from builtins.dict:

Static methods inherited from builtins.dict:

📋 class OrderedDict(builtins.dict)

Dictionary that remembers insertion order

Method resolution order: OrderedDict, builtins.dict, builtins.object

Methods defined here:

Class methods defined here:

Data descriptors defined here:

Data and other attributes defined here:

Methods inherited from builtins.dict:

Class methods inherited from builtins.dict:

Static methods inherited from builtins.dict:

🛠️ class UserDict(collections.abc.MutableMapping)

UserDict(dict=None, /, **kwargs)

Method resolution order: UserDict, collections.abc.MutableMapping, collections.abc.Mapping, collections.abc.Collection, collections.abc.Sized, collections.abc.Iterable, collections.abc.Container, builtins.object

Methods defined here:

Class methods defined here:

Data descriptors defined here:

Data and other attributes defined here:

Methods inherited from collections.abc.MutableMapping:

Methods inherited from collections.abc.Mapping:

Data and other attributes inherited from collections.abc.Mapping:

Class methods inherited from collections.abc.Collection:

Class methods inherited from collections.abc.Iterable:

🛠️ class UserList(collections.abc.MutableSequence)

UserList(initlist=None)

A more or less complete user-defined wrapper around list objects.

Method resolution order: UserList, collections.abc.MutableSequence, collections.abc.Sequence, collections.abc.Reversible, collections.abc.Collection, collections.abc.Sized, collections.abc.Iterable, collections.abc.Container, builtins.object

Methods defined here:

Data descriptors defined here:

Data and other attributes defined here:

Methods inherited from collections.abc.Sequence:

Class methods inherited from collections.abc.Reversible:

Class methods inherited from collections.abc.Iterable:

🛠️ class UserString(collections.abc.Sequence)

UserString(seq)

Method resolution order: UserString, collections.abc.Sequence, collections.abc.Reversible, collections.abc.Collection, collections.abc.Sized, collections.abc.Iterable, collections.abc.Container, builtins.object

Methods defined here:

Static methods defined here:

Data descriptors defined here:

Data and other attributes defined here:

Methods inherited from collections.abc.Sequence:

Class methods inherited from collections.abc.Reversible:

Class methods inherited from collections.abc.Iterable:

🏭 class defaultdict(builtins.dict)

defaultdict(default_factory=None, /, [...]) → dict with default factory

The default factory is called without arguments to produce a new value when a key is not present, in __getitem__ only. A defaultdict compares equal to a dict with the same items. All remaining arguments are treated the same as if they were passed to the dict constructor, including keyword arguments.

Method resolution order: defaultdict, builtins.dict, builtins.object

Methods defined here:

Class methods defined here:

Data descriptors defined here:

Methods inherited from builtins.dict:

Class methods inherited from builtins.dict:

Static methods inherited from builtins.dict:

Data and other attributes inherited from builtins.dict:

⚡ class deque(builtins.object)

deque([iterable[, maxlen]]) → deque object

A list-like sequence optimized for data accesses near its endpoints.

Methods defined here:

Class methods defined here:

Static methods defined here:

Data descriptors defined here:

Data and other attributes defined here:

🔧 FUNCTIONS

📦 namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)

Returns a new subclass of tuple with named fields.

>>> Point = namedtuple('Point', ['x', 'y'])
>>> Point.__doc__                   # docstring for the new class
'Point(x, y)'
>>> p = Point(11, y=22)             # instantiate with positional args or keywords
>>> p[0] + p[1]                     # indexable like a plain tuple
33
>>> x, y = p                        # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y                       # fields also accessible by name
33
>>> d = p._asdict()                 # convert to a dictionary
>>> d['x']
11
>>> Point(**d)                      # convert from a dictionary
Point(x=11, y=22)
>>> p._replace(x=100)               # _replace() is like str.replace() but targets named fields
Point(x=100, y=22)

📊 DATA

__all__ = ['ChainMap', 'Counter', 'OrderedDict', 'UserDict', 'UserList...

📁 FILE

/usr/lib/python3.10/collections/__init__.py

collections
📛 NAME 🚀 Quick Reference 📖 MODULE REFERENCE 📝 DESCRIPTION 📦 PACKAGE CONTENTS 📂 SUBMODULES 📚 CLASSES
🔗 class ChainMap(collections.abc.MutableMapping) 🔢 class Counter(builtins.dict) 📋 class OrderedDict(builtins.dict) 🛠️ class UserDict(collections.abc.MutableMapping) 🛠️ class UserList(collections.abc.MutableSequence) 🛠️ class UserString(collections.abc.Sequence) 🏭 class defaultdict(builtins.dict) ⚡ class deque(builtins.object)
🔧 FUNCTIONS
📦 namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)
📊 DATA 📁 FILE

Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-01 16:56 @216.73.216.239
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^