The collections module contains handy classes that allow you to manipulate iterables such as strings, tuples, lists, dictionaries, and sets. collections offers many interesting functionalities, most of which are a bit eccentric, making it unlikely that you will need to use them soon. I discuss two of them, namely the Counter class and the deque class.

A Counter object is a similar to a dictionary, which contains items as keys, and for each of the items a “count” as value. You create a Counter object by providing the sequence of which you want to count the items as argument. It has some useful methods, such as:

from collections import Counter

data = [ "apple", "banana", "apple", "banana", "apple", "cherry" ]
c = Counter( data )
print( c )
print( c.most_common( 1 ) )

data2 = [ "orange", "cherry", "cherry", "cherry", "cherry" ]
c.update( data2 )
print( c )
print( c.most_common() )

A deque object is a list that is supposed to be used as a “queue,” i.e., a list for which items are added and removed from either end of the list. It supports methods that are similar to list methods, such as append(), extend(), and pop(), which work at the right side end of the list, but also has similar methods that work at the left side end of the list, such as appendleft(), extendleft(), and popleft(). For the rest, it has the same methods that you expect a list to have. You create a deque object with the iterable which you want to turn into a deque as argument.

from collections import deque

dq = deque( [ 1, 2, 3 ] )
dq.appendleft( 4 )
dq.extendleft( [ 5, 6 ] )
print( dq )