avatarDorel Masasa

Summary

The collections module in Python's Standard Library offers powerful data types like namedtuple, deque, and Counter that enhance code readability, efficiency, and functionality.

Abstract

The article emphasizes the utility of the Python collections module, highlighting three of its most practical data types: namedtuple, deque, and Counter. It argues that these collections can significantly improve coding practices by providing features that standard Python data types like lists and tuples do not offer. namedtuple is praised for its ability to create simple classes with named attributes, enhancing code readability and maintainability. deque is presented as a versatile double-ended queue that outperforms lists when dealing with FIFO operations, offering O(1) performance for appending and popping from both ends. Lastly, Counter is described as a convenient tool for counting hashable objects, simplifying tasks that would otherwise require more code. The author encourages readers to explore these collections further and suggests that mastering them can elevate one's coding skills.

Opinions

  • The author believes that namedtuple is underused despite its benefits for code readability and debugging.
  • deque is considered an underappreciated data type that provides efficient FIFO operations compared to lists.
  • The Counter object is seen as a powerful and time-saving tool for counting repetitions within various data structures.
  • The article suggests that using these collections can lead to more readable and understandable code, which is crucial for maintenance and collaboration.
  • The author expresses enthusiasm for the collections module and its potential to streamline coding tasks, advocating for its further exploration by Python developers.

Python Collections You Should Always Be Using

Python collections is an underrated library, and it can step up your coding to the next level.

image source

Python Collections

Python collections is a module in the Python Standard Library, containing extra features and data types that you can use to your advantage.

In this article, I plan to touch the 3 most common and usable ones, but I encourage you to research more.

namedtuple

namedtuple is an easy way to represent a small simple class with no methods, it gives code readability, makes debugging easier, and saves classes on every tiny little object.

namedtuple IMO is one of the most underused objects in the collections module. As always, an example is needed:

Always remember! readability counts, and the fact the function address p1,p2 attributes by name and not by location like a standard tuple, makes it more readable and understood by any developer who will read your code afterward, or even you if you haven't touched it in a while

deque

another underused and powerful data type, the deque is like a “list with benefits”. I'm sure you're all working with lists all the time, but one of the lists most powerful features, are the order, and the pop & append. pop and append in a list is O(1), but only the standard pop & append.

lists are great for LIFO (last in first out) but really bad for FIFO (first in first out), poping location n from a list or inserting to a specific location is O(n)!

image from here

This is where deque comes in handy, you can pop and insert from both sides in O(1), and can even rotate in an easy way — keeping the order but changing the starting point.

Example:

Counter

The counter is a simple and effective object, that counts!

While doing so, giving you great speed and comfort, and great features that can save you quite a few lines of code.

It takes an input of strings, lists, anything you want, and count repetitions inside, all in just one line of code, and gives you easy access in a dictionary-style object.

I hope you didn't get tired of my examples:

We can see here, I've counted words, letters, and numbers, with only one line of code! this is a very strong tool that each python developer should know!

If you want to see another article on python STL, I recommend an other article i wrote:

Final Thoughts

The Python collections module has some great features, that can save us multiple lines of code, time and make our code much more readable!

If you enjoyed this topic, please do comment, and ill make more articles about the python standard library, I hope you enjoyed!

Python
Programming
Development
Data Science
Machine Learning
Recommended from ReadMedium
avatarAbhay Kumar
OOPs in Python

An easy guide

10 min read