Skip to main content

One Step, Any Size

O(1) means constant time: the operation takes the same number of steps whether the input has 5 elements or a million. The work does not grow at all.

This is the best complexity there is. Typical O(1) operations in Python are: reading a list element by index, reading a dictionary value by key, len(), appending one element, and simple math.

Note what O(1) does not mean: it does not mean "fast", it means "does not get slower when the input grows". One constant step can still be a slow step — but it stays one step.


List and Dictionary Lookups

A list element lives at a known position in memory, so words[2] is one jump — the computer does not walk through the elements before it. The same read costs the same in a list of one million elements.

A dictionary lookup like ages["Ben"] also takes constant time on average: the key tells the dictionary directly where the value is stored. A missing key raises a KeyError with square brackets, so lookups often use ages.get("Ben") instead — it returns the value, or None when the key is missing.

Read an element by index

The program reads one element from a 5-element list and one from a list with a million elements. Both reads are a single step — the list length plays no role.

Prompt
Write a Python program with a small list of 5 words and a big list of one million ints built with a list comprehension. Read one element from each by index and print both, to show that reading by index is one step no matter how long the list is.
Output: complete Python program
Read an element by index

Read a value by key

ages.get("Ben") finds 25 directly. The missing key "Dan" returns None, and get("Dan", 0) returns the fallback 0 instead — still in constant time.

Prompt
Write a Python program with a dictionary from names to ages holding Ana 30, Ben 25 and Cleo 41. Look up Ben with get() and print the result. Then look up the missing key Dan with get() and print the result, and once more with get() and the fallback value 0.
Output: complete Python program
Read a value by key

Appending and Simple Math

append() adds one element at the end of a list. Most of the time that is one step. (Once in a while the list must grow and gets copied, but averaged out the cost per append stays constant.)

All the small operations — assignments, additions, comparisons, swapping two values — are O(1) too. They are the "steps" that all the other complexity classes are counted in.

Append one element

append() puts "yellow" at the end. The cost does not depend on how many colors are already in the list.

Prompt
Write a Python program with a list holding red, green, blue. Print the list and its length, append yellow, then print the list and its length again.
Output: complete Python program
Append one element

Swap two values

a, b = b, a swaps the two values in one constant-time step — no helper variable needed.

Prompt
Write a Python program with two variables a = 1 and b = 2. Print them, swap them with the two-value assignment a, b = b, a, and print them again.
Output: complete Python program
Swap two values