Day 58: Python Run-Length Encoding (RLE) – Compress Strings Like a Pro with This Ultra-Simple O(n) Trick

Published: (December 8, 2025 at 12:45 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Introduction

Welcome to Day 58 of the #80DaysOfChallenges journey! This intermediate challenge implements Run-Length Encoding (RLE), the classic compression technique that turns "AAAAABBBCC" into "A5B3C2". It’s the same method used in fax machines, BMP images, and even some game formats, and it’s shockingly effective for data with long runs of repeated characters.

The solution is pure, readable, and runs in perfect O(n) time with O(1) extra space (beyond output). No libraries, no complexity, just clean Python that works.

If you’re into compression, data encoding, or just love seeing long strings shrink dramatically, this “Python RLE compressor” is beautiful, practical, and instantly useful.

Example

"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"
→ "W12B1W12B3W24B1"

Key Takeaways from Day 58: RLE Encoder Function

Function Design: Immediate Empty Check

def rle_encode(text: str) -> str:
    if not text:
        return ""

    result = []
    count = 1

Returns an empty string for empty input, perfect. Initializes a result list (for efficient building) and count = 1 (since a single character is a run of 1).

Core Loop: Run Detection Magic

    for i in range(1, len(text)):
        if text[i] == text[i - 1]:
            count += 1
        else:
            result.append(f"{text[i - 1]}{count}")
            count = 1
  • If the current character equals the previous one, increment count.
  • Otherwise, close the current run, append it to result, and start a new run with count = 1.

For "AAA":
i = 1 → count = 2, i = 2 → count = 3 → loop ends → final append "A3".

Final Run: Never Forget the Last Group!

    # Handle the final run
    result.append(f"{text[-1]}{count}")

    return "".join(result)

The loop misses the last run, so we always append it at the end. This is a classic pattern in run‑based algorithms.

Main Interactive: Real Testing

user_input = input("Enter a string to encode: ").strip()

if user_input == "":
    print("No input provided. Exiting.")
else:
    encoded = rle_encode(user_input)
    print(f"Encoded (RLE): {encoded}")

Clean input, immediate compression result. Try "aaaabbbcca""a4b3c2a1".

Summary and Reflections

This RLE encoder is short, perfect, and teaches one of the most useful patterns in data compression: run detection with counter reset. It reinforced:

  • Always handle the final run separately – common in all run‑length algorithms.
  • List + join beats string += – O(n) vs. O(n²) for long runs.
  • Real compression power"A" × 1 000 000 becomes "A1000000", tiny!

The exact technique is used in PNG, TIFF, and even some video codecs.

Advanced Alternatives

  • Implement a decode function (RLE is reversible!).
  • Handle numbers in the input (the current version treats "111" as three '1' characters).
  • Apply binary RLE for file compression.

Next Steps and Resources

Day 58 gave you real compression power in just a few lines. Try compressing your name repeated 1000 times!

What’s the longest run you ever compressed? Drop your wildest string below! 🔥

Back to Blog

Related posts

Read more »

Flatten a Nested List

Hey everyone! 👋 I know I've been a bit quiet lately. I actually came down with a pretty bad flu last week, which completely knocked me out. 🤒 That's why I mis...

Day 12: Python Programming

PART-1: LIST – Advanced Concepts List Comprehension - With condition Nested List Comprehension List Slicing Important List Methods Example: python Example...