Python collections

Filed Under: Python
Python Collections Module

Python collections module comes with with a number of container data types. These data types have different capabilities, as we will learn in this post. Let’s study about python collections module and it’s most important and widely used data types.

Python Collections Module

The collections which we will study in python collections module are:

  • OrderedDict
  • defaultdict
  • counter
  • namedtuple
  • deque

Let’s get started.

1. OrderedDict

With an OrderedDict, the order of insertion is maintained when key and values are inserted into the dictionary. If we try to insert a key again, this will overwrite the previous value for that key.

Here is a sample program to demonstrate the usage of an OrderedDict:


from collections import OrderedDict

roll_no = OrderedDict([
    (11, 'Shubham'),
    (9, 'Pankaj'),
    (17, 'JournalDev'),
])

for key, value in roll_no.items():
    print(key, value)

Let’s see the output for this program:
python OrderedDict collections
Notice that the output order was exactly the same as the order of insertion.

2. Default Dict

The default dictionary can contain duplicate keys. The advantage of using the default dictionary is that we can collect items which belong to the same key. Let’s look at a code snippet which demonstrates the same:


from collections import defaultdict

marks = [
    ('Shubham', 89),
    ('Pankaj', 92),
    ('JournalDev', 99),
    ('JournalDev', 98)
]

dict_marks = defaultdict(list)

for key, value in marks:
    dict_marks[key].append(value)

print(list(dict_marks.items()))

Let’s see the output for this program:
Python defaultdict collections
The key JournalDev was used two times and values for the same was collected once we printed the dictionary.

3. Counter

The Counter collections allow us to keep a count of all the items which are inserted into the collection with the keys. Here is a sample program to show how it works:


from collections import Counter

marks_list = [
    ('Shubham', 89),
    ('Pankaj', 92),
    ('JournalDev', 99),
    ('JournalDev', 98)
]

count = Counter(name for name, marks in marks_list)
print(count)

Let’s see the output for this program:
Python counter
This way, we were able to count the number of times a key appeared in the list.

4. Named Tuple

As we already know, Python Tuples are immutable lists. This means that a value cannot be given to a key which aready exists in the tuple. First, let’s see how a Tuple can be made in Python:


shubham = ('Shubham', 23, 'M')
print(shubham)

Let’s see the output for this program:
python tuple
We can convert this Tuple to a Named tuple by assigning a name to all values present in this tuple. This will give a lot more context to the data present as well:


import collections

User = collections.namedtuple('User', 'name age gender')
shubham = User(name='Shubham', age=23, gender='M')
print(shubham)

print('Name of User: {0}'.format(shubham.name))

Output for this program will be:
python namedtuple collections
See how we can access properties of a named tuple with the name we provide. Also, remember that the key names cannot be Python keywords.

5. Deque

A Deque is a double-ended queue which allows us to add and remove elements from both the ends. This enhances the capabilities of a stack or a queue. Here is a sample program:


import collections

name = collections.deque('Shubham')
print('Deque       :', name)
print('Queue Length:', len(name))
print('Left part   :', name[0])
print('Right part  :', name[-1])

name.remove('b')
print('remove(b):', name)

Let’s see the output for this program:
python deque collections
So, the dequeueing of the elements was done automatically. We can also insert elements in a Dequeue on a specific end. Let’s try it:


import collections

name = collections.deque('Shubham')
print('Deque       :', name)

name.extendleft('...')
name.append('-')
print('Deque       :', name)

Let’s see the output for this program:
python deque append

Conclusion

In this post, we learned how we can manage data in Python and can use the collections module to make a lot of our operations easy.

Reference: API Doc

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages