Collections in Python


In python, collections are the containers used for storing the data. The built-in data structures in python are tuples, lists, sets and dictionaries. The collections class will provide additional data structures apart from the built-in data structures.

Some of the data structures in the ‘collections’ module −

  • Counter

  • namedTuple

  • orderedDict

  • defaultDict

  • Deque

  • chainMap

Counter

It is the type of collection in which the elements will be stored as dictionary keys and the counts will be stored as dictionary values.

Whenever you want to calculate the frequency of any item in a list, traditionally you would use a variable called ‘count’ which would be initially set to zero and will be incremented by one each time you encounter the item. This is not an optimized way when the list is long. Hence, to prevent this and to count different objects at the same time, we will make use of counters.

Example

In the following example, imports the Counter method from the collections library. It then creates a string called 'a' and assigns it the value "tutorialspoint".

from collections import Counter
a="tutorialspoint"
result=Counter(a)
print(result)

Output

After executing the above program, it prints out a dictionary of each letter in 'a', along with how many times that letter appears.

Counter({'t': 3, 'o': 2, 'i': 2, 'u': 1, 'r': 1, 'a': 1, 'l': 1, 's': 1, 'p': 1, 'n': 1

values() function

The values() function will give a list of the counts associated with all the values.

Example

from collections import Counter
a=["apple","mango","cherry","apple","mango","mango"]
result=Counter(a)
print(result.values())

Output

dict_values([2, 3, 1])

most_common() function

The most_common() function is used to give a dictionary of the item/items with the maximum count. The number of values that need to be displayed has to be mentioned inside the paranthesis.

Example

from collections import Counter
a=["apple","mango","cherry","apple","mango","mango"]
result=Counter(a)
print(result.most_common(1))
print(result.most_common(2))

Output

[('mango', 3)]
[('mango', 3), ('apple', 2)]

namedtuple() function

The namedtuple() will allow us to create tuples with named fields. So, instead of accessing the items using the indices, we can access them using the dot notation.

Using the indices to access the items of a tuple can be difficult and confusing. To prevent that, we can make use of namedtuple().

Just like tuples, namedtuples are also immutable.

namedtuple takes two arguments.

Example

In the following example this program creates a namedtuple called "person" with four fields: name, place, sex and age. Then it creates an instance of the person namedtuple called id with values for each field.

from collections import namedtuple
person=namedtuple("person",["name","place","sex","age"])
id=person("kavya","Hyderabad","F","21")
print(id[1])
print(id[3])
print(id.place)
print(id.age)

Output

Hyderabad
21
Hyderabad
21

OrderedDict

OrderedDict is a dictionary which can remember the order of its items. It is used for preserving the order in which the items will be added into the list. The items can be added to the OrderedDict in various ways similar to that of the ordinary dictionary.

Example

from collections import OrderedDict
d=OrderedDict()
d['sachin']=100
d['Dhoni']=90
d['Rohit']=110
d['Kohli']=95
print(d)

Output

On executing the above code, it produces below program and prints the entire dictionary.

OrderedDict([('sachin', 100), ('Dhoni', 90), ('Rohit', 110), ('Kohli', 95)])

Example

The following program imports the OrderedDict class from the collections module and creates an OrderedDict object with four key-value pairs. The keys are 'sachin', 'Dhoni', 'Rohit' and 'Kohli'. The values are 100, 90, 110 and 95 respectively.

from collections import OrderedDict
d=OrderedDict([('sachin',100),('Dhoni',90),('Rohit',110),('Kohli',95)])
print(d)
print(d.keys())

Output

OrderedDict([('sachin', 100), ('Dhoni', 90), ('Rohit', 110), ('Kohli', 95)])
odict_keys(['sachin', 'Dhoni', 'Rohit', 'Kohli'])

defaultdict

The defaultdict has all the functionalities of a dictionary with the additional feature where it will never give a KeyError. In a dictionary, if you try to access or modify the keys that do not exist, you will get a KeyError. Whereas, defaultdict always assigns a default value if the key does not exist. Hence, defaultdict is used for handling missing keys.

Example

The following program imports the defaultdict class from the collections module. It then creates a new dictionary d using defaultdict and sets it to take in integer values. Three key-value pairs are added to the dictionary, with 'Sachin' having a value of 90, 'Dhoni' having a value of 80, and 'Virat' having a value of 95.

from collections import defaultdict
d=defaultdict(int)
d['Sachin']=90
d['Dhoni']=80
d['Virat']=95
print(d)
print(d['Dhoni'])
print(d['Rohit'])

Output

defaultdict(, {'Sachin': 90, 'Dhoni': 80, 'Virat': 95})
80
0

Since ‘Rohit’ is not defined and ‘int’ is given as the parameter, the default value will be 0.

Example

from collections import defaultdict
d=defaultdict(float)
d['Sachin']=90
d['Dhoni']=80
print(d['Rohit'])

Output

0.0

Updated on: 24-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements