liveBook · Manning

Linear time complexity

As explained in Practices of the Python Pro

Linear time complexity is an asymptotic running-time growth rate proportional to input size, or to the combined sizes of multiple inputs, up to a constant factor.

Linear time complexity describes work that grows in proportion to input size. In big O notation, it is written O(n): processing twice as many items takes roughly twice as long. section 4.1.2

A single loop over a list or other sequence is usually O(n). Several fixed passes remain linear: they make the line steeper but do not change its overall growth rate. section 4.1.2

Recognize linear work in familiar Python tasks:

  • Printing or summing every item in a sequence. section 4.1.2
  • Running multiple fixed loops over the same sequence. section 4.1.2
  • Iterating through a dictionary or set; direct access and membership checks behave differently and can be constant time. section 4.2.1
  • Using lists when membership checks or insertion at arbitrary positions may become slower as the list grows. section 4.2.2

A simple linear pass

for name in names:\n print(name)\n\nThe loop visits each name once, so its running time grows with the number of names. section 4.1.2

def has_duplicates(sequence):
    for index1, item1 in enumerate(sequence):        #1
        for index2, item2 in enumerate(sequence):    #2
            if item1 == item2 and index1 != index2:  #3
                return True
    return False

Compare a single linear pass with nested loops, where work can grow faster as the input expands. section 4.1.2

How Practices of the Python Pro covers it

How deep it goes
Explains O(n) through line analogies, loop examples, repeated passes, and contrasts with higher-order growth.
Examples
Uses name lists, repeated loops, duplicate detection, and favorite-color file processing.
What you can do after
Evaluate code for likely O(n) behavior and choose data representations suited to scaling work.
How it connects
Connects loops, asymptotic analysis, space complexity, and Python data-type performance.

Also explained in

Compare how 8 books explain linear time complexity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.