Skip to content

Python Collections

How to Use Python to Count Words and Generate Word Frequencies Cover Image

Python: Count Words in a String or File

In this tutorial, you’ll learn how to use Python to count the number of words and word frequencies in both a string and a text file. Being able to count words and word frequencies is a useful skill. For example,

Python Counter Collections Count Items Cover Image

Python’s Counter: Count Items with Collections Counter

In this tutorial, you’ll learn how to use the Python Counter class from the collections module to count items. The Counter class provides an incredibly pythonic method to count items in lists, tuples, strings, and more. Because counting items is

Python List Prepend Cover Image

Prepend to a Python a List (Append at beginning)

In this tutorial, you’ll learn how to use Python to prepend a list. While Python has a method to append values to the end of a list, there is no prepend method. By the end of this tutorial, you’ll have

Python Named Tuples Cover Image

How to Use Python Named Tuples

The Python collections module provides a helpful factory function, namedtuple, which allows you to create named tuples in Python. The idea behind named tuples is to make working with tuples more pythonic by allowing you to access data in the

Python Defaultdict Overview and Examples Cover image

Python Defaultdict: Overview and Examples

In this tutorial, you’ll learn about the Python defaultdict objects, which is part of the collections library. The object overrides the default dictionary behavior of throwing a KeyError when you try to access a key that doesn’t exist. You’ll get