Python collections Module: namedtuple to deque

Published on: June 3, 2026
Reading time: 4 minutes
Uso do módulo collections para estruturas avançadas em Python

The collections module is one of the most powerful and underused tools in Python’s standard library. While basic types like lists and dictionaries are sufficient for most simple tasks, collections offers specialized alternatives that improve performance and code readability. Mastering these structures is a key step for any developer who wants to move beyond the basics and reach technical mastery in Python.

The module provides data containers that extend built-in types. Think of organizing a workshop: plain lists are like open boxes where you throw everything, while collections offers labeled drawers and specialized holders for each tool. According to PEP 8, choosing the right data structure is a fundamental good practice. Choosing the wrong one is often why Python code feels slow in certain contexts.

namedtuple: tuples with field names

Accessing tuple data by numeric index like person[0] or person[1] is confusing and error-prone. namedtuple solves this by letting you assign names to fields. It creates a lightweight class-like structure that uses far less memory than a full object, perfect for database records or coordinates:

Python
from collections import namedtuple

Counter: counting elements effortlessly

Counter is probably the most beginner-friendly tool in collections. Instead of writing a loop with if/else logic to tally items, just pass your iterable to Counter. It works like a dictionary subclass where items are keys and their counts are values. Extremely useful for text analysis or log processing:

Python
from collections import Counter

defaultdict: dictionaries with automatic defaults

defaultdict eliminates the KeyError problem. When you access a key that does not exist, instead of crashing it automatically creates a default value using a factory function you specify. This simplifies grouping logic significantly:

Python
from collections import defaultdict

deque: efficient double-ended queue

deque (double-ended queue) is the go-to when you need to frequently add or remove items from both ends of a sequence. A Python list is O(n) for insert(0, ...) operations because every element shifts. deque is O(1) for both appendleft and popleft, making it ideal for queues and breadth-first search. See also Python generators for another memory-efficient iteration pattern.

Python
from collections import deque

Quick reference: when to use each

ToolBest use case
namedtupleImmutable records with named fields (coordinates, DB rows)
CounterCounting elements in any iterable
defaultdictGrouping data without KeyError
dequeFast append/pop at both ends (queues, sliding windows)
OrderedDictWhen insertion order must be preserved (pre-3.7)

The official Python collections documentation covers all available types with full method references. Reading it is highly recommended once you are comfortable with the basics above.

Frequently asked questions

Is the collections module always faster than plain lists?

Not always. For small datasets, the difference is negligible. The advantage becomes significant with large volumes of data or when performing operations that lists handle poorly, like inserting at the front.

Can I use Counter with strings?

Yes. Counter("hello") returns Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1}), counting each character.

Does deque support indexing?

Yes, but random access (like deque[5]) is O(n), unlike lists which are O(1). Use deque only when you frequently access both ends, not random positions.

Knowing when to reach for collections over plain lists and dicts is a hallmark of experienced Python developers. Start with Counter and defaultdict, as they solve the most common everyday problems with minimal code change.

Share:

Facebook
WhatsApp
Twitter
LinkedIn

Article content

    Related articles

    Uso do with para abrir arquivos com segurança em Python
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python with Statement: Safe File Handling

    Learn how Python's with statement safely opens files: automatic close, read/write modes, CSV handling, multiple files, and context manager basics.

    Ler mais

    Tempo de leitura: 3 minutos
    03/06/2026
    Operações matemáticas usando o módulo math em Python
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python math Module: Mathematical Operations

    Learn Python's math module: sqrt, pow, ceil, floor, trig functions, logarithms, constants like pi and e, and special numeric checks

    Ler mais

    Tempo de leitura: 3 minutos
    03/06/2026
    Uso do módulo time para controlar tempo em scripts Python
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python time Module for Beginners

    Learn how Python's time module works: Unix timestamp, time.sleep() for pauses, localtime(), strftime() for date formatting, and measure execution time.

    Ler mais

    Tempo de leitura: 3 minutos
    03/06/2026
    Uso da função zip para combinar listas em Python
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python zip() Function: Beginner’s Guide

    Learn how Python zip() works: combine iterables, loop over multiple lists, handle different lengths, unzip with *, and use zip_longest

    Ler mais

    Tempo de leitura: 2 minutos
    03/06/2026
    Criação de geradores eficientes usando yield em Python
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python yield: Create Efficient Generators

    Learn how Python yield works to create memory-efficient generators, process large files, build infinite sequences, and use generator pipelines.

    Ler mais

    Tempo de leitura: 6 minutos
    03/06/2026
    Uso do operador walrus para atribuições inline em Python
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python Walrus Operator (:=) Explained with Examples

    Learn how Python's walrus operator (:=) works for assignment expressions inside if, while, and list comprehensions, with practical examples and

    Ler mais

    Tempo de leitura: 3 minutos
    03/06/2026