COLLECTION MODULE IN PYTHON

  • The Python Collection module is defined as a container that is used to store collections of data, for example – list, dict, set, and tuple, etc.
  • It was introduced to improve the functionalities of the built-in collection containers.
  • Python collection module was first introduced in its 2.4 release.
  • There are different types of collection modules which are as follows:

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.

Example:

a= (‘Jan’,24, ‘F’)

print(a)

Output: >>(‘Jan’,24,’F’)

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.

Example:

import collections

dl=collections.OrderedDict()

dl[‘A]=10

dl[‘C]=12

dl[‘B]=11

dl[‘D]=13

for k,v in dl.items():

print(k,v)

Output: >>A 10 C 12 B 11 D 13

defaultdict()

The Python defaultdict() is defined as a dictionary like object. It is a subclass of the built-in dict class. It provides all methods provided by dictionary but takes the first argument as a default data type.

Example:

from collections import defaultdict

number= defaultdict(int)

number[‘one’]=1

number[‘two’]=2

print(number[‘three’])

Output: >>0

Counter()

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

Example:

from collections import Counter

c=Counter()

list=[1,2,3,4,5,7,8,5,9,6.10]

Counter({1:5,2:4})

list=[1,2,4,7,5,1,6,7,9,1]

print(c[1])

Output: >> 3

deque()

The Python deque() is a double ended queue which allows us to add and remove elements from both the ends.

Example:

from collections import deque

list=[“x”,”y”,”z”]

deq=deque(list)

print(deq)

Output: >>deque([‘x’.’y’,’z’])

Leave a comment

Design a site like this with WordPress.com
Get started