liveBook · Manning

Constant time

As explained in Python Workout, Second Edition

Constant time is a computational complexity in which execution time does not grow with the size of the input.

Constant time, written O(1), describes work whose execution time does not grow as the input grows. A direct lookup can take the same amount of time whether the collection has 2, 25, or 250 items. section 5.1

Direct lookup and collisions

An office numbered for each initial sends visitors directly to the relevant location. Python dicts use a similar idea: a key passes through a hash function, which identifies where its key–value pair is stored. Retrieving d['a'] hashes 'a' and checks the indicated slot. Collisions require additional handling, such as sharing a location and searching within it. section 5.1

How Python Workout, Second Edition covers it

How deep it goes
Defines O(1), illustrates its scaling behavior, and sketches hashing and collisions while deferring several implementation details.
Examples
Uses numbered offices, alphabetic initials, duplicate surnames, and d['a'] = 1 to demonstrate direct lookup and collisions.
What you can do after
Apply it via Python dicts by relating key hashing to immediate storage-slot lookup and recognizing collision handling.
How it connects
Connects constant time with hashing, hash functions, dictionaries, mappings, collisions, cryptography, and security.

Also explained in

Compare how 7 books explain constant time →

Related concepts

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