Collections in python.

Insha.
2 min readApr 18, 2021

# Collections are the inbuilt module in python

#which give more functionalites and features than inbuilt data types — list, set, dictionary,tuple etc.

#For using collections you have to import collections module

#Few the popular collections functions.

Counter

defaultdict

OrderedDict

deque

ChainMap

namedtuple()

#Counter function of Collections module

#counter function argument take sequences and return dictionary with the key and values as there number of

#occurences.import randoml1=[random.randint(1,5) for i in range(8)]print(“List1:”,l1)from collections import Countercount1=Counter(l1)print(“count1:”,count1)
###################
List1: [5, 1, 3, 4, 2, 3, 5, 3]
count1: Counter({3: 3, 5: 2, 1: 1, 4: 1, 2: 1})

# using dictinary comprehension checking the count

from collections import Counterimport randomdict1={k:v for k in range(10) for v in range(10)}#print(dict1)dict2={random.randint(1,7):random.randint(15,18) for k in range(10) for v in range(10)}print(dict2)count2=Counter(dict2)print(“count2:”,count2)count3=Counter(dict2.values())print(“Count3:”,count3)
#################################
{3: 18, 6: 17, 4: 15, 1: 17, 5: 17, 2: 15, 7: 17}
count2: Counter({3: 18, 6: 17, 1: 17, 5: 17, 7: 17, 4: 15, 2: 15}) Count3: Counter({17: 4, 15: 2, 18: 1})

#Element- most_common

#You can sort it according to the number#of counts in each element using most_common() function of the Counter object.l3=[random.randint(1,3) for k in range(15)]print(l3)from collections import Countercur=Counter(l3)print(“cur:”,cur)print(“cur.most_common:”,cur.most_common())#################################################
[3, 3, 1, 1, 1, 1, 2, 3, 2, 2, 1, 3, 3, 3, 1]
cur: Counter({3: 6, 1: 6, 2: 3})
cur.most_common: [(3, 6), (1, 6), (2, 3)]

# defaultdict second example

from collectors import defaultdictlist1=”hi hello hi hi hi helllo hi bye python great programming”.split()count=defaultdict(int)for name in list1:count[name]+=1print(“count[name]:”,count[name])print(“count:”,count)
####################################
count[name]: 1 count[name]: 1 count[name]: 2 count[name]: 3 count[name]: 4 count[name]: 1 count[name]: 5 count[name]: 1 count[name]: 1 count[name]: 1 count[name]: 1 count: defaultdict(<class 'int'>, {'hi': 5, 'hello': 1, 'helllo': 1, 'bye': 1, 'python': 1, 'great': 1, 'programming': 1})

#nameddtuple()

The namedtuple() returns a tuple with names for each position

in the tuple. One of the biggest problems with ordinary tuples is that you have to remember the index of each field of a tuple object.

#simple tuple

tup1=(1,2,3,”hi”,”hello”,”bye”,”78",998.667,”oy”)print(tup1)print(tup1[4])
############
(1, 2, 3, 'hi', 'hello', 'bye', '78', 998.667, 'oy')
hello

################

from collections import namedtupletup1=namedtuple(‘student’,”name,roll,address”)student1=tup1(“ram1”,3,”india”)student2=tup1(“stud2”,4,”USA”)print(“tup1:”,tup1)print(“student1.name:”,student1.name)print(“student2.address:”,student2.address)
#################################
tup1: <class '__main__.student'>
student1.name: ram1
student2.address: USA

--

--