🐍 Python Course

Lists

1.2 Lists

Core operations, interview reasoning, and real-world usage

Lists are one of the most important Python data structures.

If strings are the most common sequence of characters, lists are the most common sequence of objects.

You will use lists constantly in:

  • backend APIs
  • data processing
  • loops
  • algorithm interviews
  • temporary storage
  • queue / stack patterns

A large number of interview questions are really list problems in disguise.

Examples:

  • remove duplicates
  • reverse data
  • sliding window
  • merge intervals
  • find duplicates
  • sort objects
  • transform data

This chapter focuses on how to reason about lists as dynamic arrays, not just syntax.


1.2.1 Mental model: what a list really is

A list is an ordered, mutable sequence.

Example

nums = [1, 2, 3]

This means two important things:

Ordered

Each item has a stable position.

nums[0]   # 1
nums[1]   # 2

Order matters.

This distinguishes it from sets.

Mutable

Unlike strings, lists can be changed in place.

nums[0] = 10

Now:

[10, 2, 3]

This is extremely important for interview questions involving references and mutation.


1.2.2 append()

One of the most used list methods.

nums = [1, 2, 3]
nums.append(4)

Result

[1, 2, 3, 4]

What it does

Adds one item to the end of the list.

Important interview distinction

append() adds the object as a single element.

nums = [1, 2]
nums.append([3, 4])

Result

[1, 2, [3, 4]]

This is a common interview trap.

Complexity

Average complexity:

O(1)

More precisely:

O(1) amortized

Because resizing happens occasionally.

This is a strong interview answer.


1.2.3 extend()

Very important distinction from append().

nums = [1, 2]
nums.extend([3, 4])

Result

[1, 2, 3, 4]

What it does

Adds all elements from an iterable individually.

Compare append() vs extend()

append()

nums = [1, 2]
nums.append([3, 4])

Result:

[1, 2, [3, 4]]

extend()

nums = [1, 2]
nums.extend([3, 4])

Result:

[1, 2, 3, 4]

This difference is frequently asked in interviews.


1.2.4 pop()

Used to remove and return an element.

nums = [1, 2, 3]
last = nums.pop()

Result

last = 3
nums = [1, 2]

Pop specific index

nums.pop(0)

Removes the first element.

Important complexity question

pop()

nums.pop()

Complexity:

O(1)

pop(0)

nums.pop(0)

Complexity:

O(n)

Because all elements shift left.

This is a very common interview question.


1.2.5 Sorting

Sorting is one of the most tested list operations.

sort()

nums = [3, 1, 2]
nums.sort()

Result

[1, 2, 3]

Important

sort() modifies the list in place.

sorted()

nums = [3, 1, 2]
new_nums = sorted(nums)

The original list remains unchanged.

Interview question: sort() vs sorted()

Strong answer:

  • sort() mutates the original list
  • sorted() returns a new list

Sorting reverse

nums.sort(reverse=True)

Sorting by key

people = [
    {"name": "A", "age": 30},
    {"name": "B", "age": 25}
]

people.sort(key=lambda x: x["age"])

This appears constantly in interviews.

Complexity

Python sorting:

O(n log n)

Python uses Timsort.

Strong verbal answer:

  • stable
  • optimized
  • excellent for partially sorted data

1.2.6 Slicing

Lists use the same slicing syntax as strings.

nums = [1, 2, 3, 4, 5]

nums[1:4]

Result

[2, 3, 4]

Important rules

  • start inclusive
  • stop exclusive

Common slices

nums[:3]
nums[3:]
nums[::-1]
nums[::2]

Reverse list

nums[::-1]

Very common interview solution.

Copy a list

copy_nums = nums[:]

This creates a shallow copy.


1.2.7 List comprehensions

One of the most important Pythonic tools.

Basic syntax

squares = [x * x for x in range(5)]

Result

[0, 1, 4, 9, 16]

Mental model

Equivalent to:

squares = []
for x in range(5):
    squares.append(x * x)

Conditional comprehensions

evens = [x for x in range(10) if x % 2 == 0]

Result

[0, 2, 4, 6, 8]

Nested comprehensions

pairs = [(x, y) for x in range(2) for y in range(2)]

Result

[(0, 0), (0, 1), (1, 0), (1, 1)]

1.2.8 Important list operations

Membership

3 in nums

Complexity:

O(n)

Length

len(nums)

Iteration

for num in nums:
    ...

1.2.9 Common interview problems

Lists are used in:

  • two pointers
  • sliding window
  • stack problems
  • frequency counting
  • sorting
  • interval merging
  • duplicate detection

This chapter is foundational.


Verbal interview questions

  • Difference between append() and extend()
  • Why is pop(0) slower than pop()
  • Difference between sort() and sorted()
  • Explain shallow copy with slicing
  • Why use comprehension over manual loop

Coding drills

Drill 1: remove duplicates

def remove_duplicates(nums: list[int]) -> list[int]:
    ...

Drill 2: flatten nested list

def flatten(matrix: list[list[int]]) -> list[int]:
    ...

Drill 3: sort people by age

def sort_people(people: list[dict]) -> list[dict]:
    ...

Use:

sorted(..., key=...)