Skip to Content
Intuitive Python
book

Intuitive Python

by David Muller
May 2021
Intermediate to advanced
142 pages
3h 37m
English
Pragmatic Bookshelf
Content preview from Intuitive Python

Using the collections Module for Concise Code

The collections standard library module is a standby for many Python developers. It provides a number of utilities (particularly defaultdict, Counter, and namedtuple) that help reduce boilerplate code and make it possible to write concise Python. After you read through this section on collections, you will be familiar with some of the utilities in the module and be prepared to include them in your projects.

Write Concise Code with defaultdict

defaultdict is a convenience provided by the collections module. defaultdict allows you to add values to dictionaries concisely and reduce boilerplate code.

Consider, for example, if you had a program that used a dict to map file owners to files. The following ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Pythonic Programming

Pythonic Programming

Dmitry Zinoviev
Python for Geeks

Python for Geeks

Muhammad Asif

Publisher Resources

ISBN: 9781680508635Errata Page