01 / 20
\( O(n) \)

Big-O for
Lists & Dicts

Understand how loops, nested loops, and common operations on Python lists and dicts affect your program's speed: from \( O(1) \) to \( O(n^2) \).

Python

What Is Big-O Notation?

Big-O describes how the number of steps your code takes grows as the input size \( n \) increases. It ignores constants and focuses on the worst-case growth rate.

\( O(1) \)
Constant: same speed no matter how big \( n \) is
\( O(n) \)
Linear: double the input, double the work
\( O(n^2) \)
Quadratic: double the input, quadruple the work

Visualizing Growth Rates

Input size (n) Steps 20 40 60 80 100
\( O(1) \)
\( O(n) \)
\( O(n \log n) \)
\( O(n^2) \)

Why Big-O Matters in Python

Python makes it easy to write clean code, but hidden performance traps lurk inside common operations. Knowing Big-O helps you predict whether your program will finish in milliseconds or minutes.

1
Spot bottlenecks: a single \( O(n^2) \) loop inside an \( O(n) \) loop creates \( O(n^3) \) work
2
Choose wisely: checking x in my_list is \( O(n) \), but x in my_dict is \( O(1) \)
3
Scale confidently: code that works on 100 items might crash on 1,000,000 without the right complexity

The Loop Rule

Single Loop for i in range(n): do_work()
\( O(n) \)
n iterations Nested Loop for i in range(n): for j in range(n): do_work()
\( O(n^2) \)
\( n \times n \) iterations

List vs Dict Membership

List: x in my_list 3 7 1 9 5
Scan one by one: \( O(n) \)
Dict: x in my_dict 0: 3 1: 7 2: 5 ... hash(5)
Jump directly: \( O(1) \)
With 1,000,000 items: List: up to 1,000,000 checks Dict: exactly 1 check

Sequential vs Nested Loops

Sequential (Add)
Two loops one after another:
\( O(n) + O(n) = O(2n) = \) \( O(n) \)

Constants are dropped, so sequential loops stay linear.
Nested (Multiply)
One loop inside another:
\( O(n) \times O(n) = \) \( O(n^2) \)

Each iteration of the outer loop runs the entire inner loop.

Python Operations Cheat Sheet

Operation List Dict / Set
Index / Key lookup \( O(1) \) \( O(1) \)
Membership: x in \( O(n) \) \( O(1) \)
Append / Insert end \( O(1) \) \( O(1) \)
Insert at position 0 \( O(n) \) N/A
Delete by value \( O(n) \) \( O(1) \)
Iteration (for loop) \( O(n) \) \( O(n) \)
Sort \( O(n \log n) \) N/A

How to Analyze Your Code

1
Identify loops: count how many loops exist and whether they are sequential or nested
2
Label each loop: what does it iterate over? If it depends on \( n \), it costs \( O(n) \)
3
Check operations inside: is there a hidden \( O(n) \) like x in my_list inside the loop?
4
Combine: multiply nested complexities, add sequential ones
5
Drop constants: \( O(3n) \rightarrow O(n) \), \( O(n^2 + n) \rightarrow O(n^2) \). Keep only the dominant term

Example 1: Simple Loop

def find_max(nums): best = nums[0] # O(1) for x in nums: # O(n) ← one loop if x > best: best = x # O(1) return best

Example 2: Nested Loop

def has_duplicates(nums): for i in range(len(nums)): # O(n) for j in range(i+1, len(nums)): # O(n) if nums[i] == nums[j]: # O(1) return True return False

Example 3: Hidden \( O(n) \) Inside a Loop

# Looks simple, but watch the "in" operator! def common_elements(list_a, list_b): result = [] for x in list_a: # O(n) if x in list_b: # O(n) ← hidden scan! result.append(x) # O(1) return result

The Set/Dict Fix Pattern

# BEFORE: O(n²) — membership check on list for x in data: if x in big_list: # O(n) per check process(x) # AFTER: O(n) — membership check on set big_set = set(big_list) # O(n) one-time cost for x in data: if x in big_set: # O(1) per check process(x)

Dropping Constants & Lower Terms

Big-O only cares about the dominant term as \( n \rightarrow \infty \). Constants and smaller terms vanish:

\( O(3n + 5) \)
Simplifies to \( O(n) \)
\( O(n^2 + n) \)
Simplifies to \( O(n^2) \)
\( O(500) \)
Simplifies to \( O(1) \)

Common Mistakes

✕ Ignoring "in" on Lists
Thinking x in my_list is \( O(1) \). Lists require a full scan: it is \( O(n) \).
✕ Adding Instead of Multiplying
Writing \( O(n + n) \) for nested loops. Nested means multiply: \( O(n) \times O(n) = O(n^2) \).
✕ Forgetting to Drop Constants
Reporting \( O(2n) \) or \( O(n/2) \) as the answer. Big-O drops all constants: both are \( O(n) \).

Question 1 of 5

What is the time complexity of checking x in my_list where my_list has \( n \) elements?

Question 2 of 5

Two sequential (not nested) loops each run \( n \) times. What is the overall complexity?

Question 3 of 5

What is the overall time complexity of this code?

Question 4 of 5

What is the time complexity?

Question 5 of 5

A student changes list_b to a set. What does the complexity become?