ReviseAlgo Logo

Lists

List Basics

Creating and using lists

Interview: Most commonly used data structure — essential for all Python interviews

Last Updated: June 12, 2026 10 min read

Lists are Python's most versatile built-in data structure — mutable, ordered, heterogeneous sequences that can hold any type of object. They are implemented as dynamic arrays under the hood, giving O(1) indexing and amortized O(1) append, but O(n) insertion/deletion at arbitrary positions.

Creating Lists

  • Literal syntax: [1, 2, 3] — most common
  • Constructor: list("abc")['a', 'b', 'c']
  • Empty list: [] or list()
  • Heterogeneous: [1, "hello", 3.14, [5, 6], None] — can mix types
  • From range: list(range(5))[0, 1, 2, 3, 4]

Accessing Elements

  • Positive indexing: lst[0] (first), lst[2] (third)
  • Negative indexing: lst[-1] (last), lst[-2] (second to last)
  • Slicing: lst[1:4] (elements at index 1, 2, 3)
  • Out of bounds: lst[100] raises IndexError

Performance Characteristics

  • Index access: O(1) — direct memory offset
  • Append: Amortized O(1) — overallocation strategy
  • Insert at beginning: O(n) — must shift all elements
  • Delete at index: O(n) — must shift remaining elements
  • Membership (in): O(n) — linear scan
  • Length: O(1) — stored internally

Performance Tip

If you frequently insert/delete at the beginning, use collections.deque instead — it provides O(1) operations at both ends.

Interview Tip

Know the difference between list.append() and list.extend(): append adds one element, extend adds all elements from an iterable. Also know that + creates a new list while extend() modifies in place.

Use Cases

Storing ordered collections of items

Building result sets from loops and comprehensions

Implementing stacks (append + pop)

Matrix/2D data representation with nested lists

Common Mistakes

Using + in a loop to build a list (O(n^2)) — use append() or a comprehension instead

Creating a list with [[0]*n]*m for 2D arrays (creates shared references)

Modifying a list while iterating over it (use list copy or comprehension)

Confusing list.sort() (in-place, returns None) with sorted() (returns new list)