Collections — Container Datatypes - Python




Collection Module in Python

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

NameDescription
namedtuple()factory function for creating tuple subclasses with named fields
deque()list-like container with fast appends and pops on either end
ChainMap()dict-like class for creating a single view of multiple mappings
Counter()dict subclass for counting hashable objects
OrderedDict()dict subclass that remembers the order entries were added
defaultDict()dict subclass that calls a factory function to supply missing values
UserDict()wrapper around dictionary objects for easier dict subclassing
UserList()wrapper around list objects for easier list subclassing
UserString()wrapper around string objects for easier string subclassing

EXAMPLE               


namedTuple()

The python namedtuple() function returns a tuple-like object with names for each position in the tuple. It was used to eliminate the problem of remembering the index of each field of a tuple object in ordinary tuples.

deque()

The deque() in Python is the doubly ended queue in which you can remove or add elements at both the ends.

Counter()

The Python Counter is a subclass of dictionary object which helps to count hashable objects.

ChainMap()

The ChainMap class only makes updates (writes and deletions) to the first mapping in the chain while lookups will search the full chain.

OrderedDict()

The Python OrderedDict() is similar to a dictionary object where keys maintain the order of insertion. If we try to insert key again, the previous value will be overwritten for that key.

defaultDict()

Returns a new dictionary-like object. defaultdict is a subclass of the built-in dict class. It overrides one method and adds one writable instance variable.

--Thanks for Reading--

1 comment:

  1. Please Share and Follow :)

    Website Link (Under Development - Programming Tutorials) - http://bestpythontutorials.com/

    ReplyDelete