Lists, Arrays & Strings: Big‑O Overview

Lists, Arrays & Strings: Big‑O Overview

🧠 Static Arrays, Dynamic Arrays, and Strings – Big O Complexity

  • The last element of a list is accessed at index len(arr) - 1.
  • Python lists are mutable, meaning you can change, append, or remove elements.
  • A static array has fixed size and cannot grow or shrink (Python uses dynamic lists).
  • Static arrays support operations like indexing, updating, and iterating.
  • Static arrays are similar to memory layout in low-level languages like C.
  • Python lists are dynamic arrays that resize automatically when needed.
  • Python internally resizes lists by doubling capacity (power of 2 growth).
  • Amortized time complexity means average cost per operation stays low even if some operations are expensive.

🧠 Practical Insight: Understanding Python list resizing helps you avoid costly insertions or memory waste in tight loops or high-performance tasks.


🐍 Python List Big‑O Cheat Sheet

OperationTime ComplexitySpace ComplexityNotes
arr[i]O(1)O(1)Random index access
arr[i] = xO(1)O(1)Update value
arr.append(x)O(1) (amortized)O(1)Append at end
arr.insert(0,x)O(n)O(n)Insert at beginning
arr.insert(i,x)O(n)O(n)Insert at middle
arr.pop()O(1)O(1)Remove last element
arr.pop(i)O(n)O(n)Remove from index i
arr.remove(x)O(n)O(1)Find and remove first x
x in arrO(n)O(1)Membership test
arr.index(x)O(n)O(1)Index of first occurrence
arr.count(x)O(n)O(1)Count occurrences
del arr[i]O(n)O(n)Delete by index
arr.sort() / sorted(arr)O(n log n)O(n) (Timsort)Stable sort
arr.reverse()O(n)O(1)In-place reverse
arr.copy()O(n)O(n)Shallow copy
len(arr)O(1)O(1)Constant-time length
IterationO(n)O(1)for x in arr
arr + arr2O(n + m)O(n + m)Concatenation
arr * kO(nk)O(nk)Repeat list k times

✅ Methods That Return a Value

MethodReturns What?Example
arr.copy()New shallow copycopy_arr = arr.copy()
arr.pop()Last elementlast = arr.pop()
arr.index(x)First occurrence indexi = arr.index(3)
arr.count(x)Count of xc = arr.count(4)
x in arrTrue or Falsefound = x in arr
sorted(arr)New sorted lists = sorted(arr)
arr + arr2Concatenated listcombined = arr + arr2

Pro Tip: Always assign the return value when using these methods. Otherwise, you lose the result.


🚫 Methods That Modify In‑Place

MethodIn-place?Correct Usage
arr.append(x)arr.append(x)
arr.sort()arr.sort()
arr.reverse()arr.reverse()
del arr[i]del arr[2]
arr.clear()arr.clear()
arr.insert(i,x)arr.insert(1, 42)
arr.remove(x)arr.remove(5)

🚫 Common Mistake: Don’t assign arr.sort() or arr.reverse() to a variable — it will return None.


🧵 String Big‑O Cheat Sheet

  • Python strings are immutable — any “modification” creates a new string.
Operation / MethodTimeSpaceExample
s[i]O(1)O(1)char = s[2]
s1 + s2 (concat)O(n+m)O(n+m)res = s1 + s2
s.lower()O(n)O(n)s = s.lower()
s.replace(a, b)O(n)O(n)new = s.replace(...)
s.find(x) / s.index(x)O(n)O(1)i = s.find("a")
s.split() / s.join()O(n)O(n)words = s.split()
'x' in sO(n)O(1)'cat' in s

✅ String Methods That Return a Value

Method / OperationReturnsExample
s.lower(), s.upper()new strlow = s.lower()
s.replace(a, b)new strnew = s.replace(...)
s.strip()trimmed strs = s.strip()
s.find(), s.count()intcount = s.count("a")
'x' in sboolif 'a' in s:
s.split()listwords = s.split()

🚫 Strings Cannot Be Modified In-Place

Wrong: s.strip()
Right: s = s.strip()

s = " Hello "
s.strip()      # No effect
print(s)       # " Hello "

s = s.strip()  # Now it's trimmed
print(s)       # "Hello"

✅ Problems Solved This Week