Start learning free

LESSON 9 OF 69 · PROJECTS: COMMAND-LINE TOOLS

wc v1 in Python: Counting Lines with len

Practice Read the full explanation

Build a tiny version of the Unix wc command. Given a list of lines, return the total number of lines as an integer.

Function signature:

def countLines(self, lines: List[str]) -> int:

Example:

Input: lines = ["hello", "world", "foo", "bar", "baz"]

Output: 5

Every entry of the list is one line, even an empty string "". An empty list has 0 lines.


The number of lines is just the length of the input list. Python has a built-in for that — no loop needed.

A one-liner using len:
class Solution:
    def countLines(self, lines):
        return len(lines)

STEP 1 OF 1
    Ctrl + Enter to check

    PYTHON · PROJECTS: COMMAND-LINE TOOLS

    Wc V1 in Python, explained

    Problem overview

    This is the first version of a small wc project, a copy of the Unix word-count command. In this version the method countLines(self, lines) receives a list of lines and returns how many there are, as an integer.

    For ["hello", "world", "foo", "bar", "baz"] the answer is 5. Every entry of the list counts as one line, even an empty string. An empty list has 0 lines.

    Approach: the length of the list is the answer

    The input is already split into lines, one list item per line. Counting lines therefore means counting items, and Python's built-in len does that directly. No loop and no counter variable are needed.

    A loop that adds 1 for each item would give the same number, but it repeats work that len already does, and it adds code in which a bug could hide.

    If the text arrives as one string instead, splitting it on newlines first gives the list: "a\n\nb".split("\n") is ['a', '', 'b'], which has 3 lines, the middle one blank. In this lesson that splitting has already been done for you, so the method only counts.

    Step by step

    The lesson has one step: inside countLines, return how many entries lines has. That is len(lines). The method returns the number and does not print it.

    The solution in Python

    class Solution:
        def countLines(self, lines):
            return len(lines)
    

    Some details worth knowing:

    • len counts the items of the list, not the characters inside them. For ["hello", "world"] it gives 2, while len("hello") on a single string gives 5.
    • It is fast. A list stores its size, so len does not walk through the items, however long the list is.
    • It gives an integer. The result is an int, which is what the method's signature promises.
    • It counts entries, not their content. An empty string is one entry, so len([""]) is 1.
    • Nested lists count once each. len([["a", "b"], ["c"]]) is 2, because each inner list is a single item of the outer one.

    Edge cases and common mistakes

    • An empty list. countLines([]) returns 0.
    • Blank lines. countLines([""]) returns 1, and countLines(["", "", ""]) returns 3. A blank line is still a line, so skipping empty strings would give the wrong answer.
    • Newlines inside an entry. The method counts list entries, not \n characters. An entry such as "a\nb" still counts once: countLines(["a\nb"]) returns 1.
    • Counting characters by mistake. Calling len on one line, such as len(lines[0]), gives the length of that text, not the number of lines.
    • Printing instead of returning. The lesson's tests read the returned value.
    • Off-by-one from a counter. A hand-written loop that starts the counter at 1, or adds after the loop, can be off by one. len avoids the whole class of mistakes.

    Testing

    Try these inputs in the editor above and compare the returned values.

    • ["hello", "world", "foo", "bar", "baz"] gives 5.
    • [] gives 0.
    • [""] gives 1.
    • ["", "", ""] gives 3.
    • ["a\nb"] gives 1.

    The next version builds on this one.

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free