Python Tutorial 0/52 lessons ~6 min read Lesson 17

    Lists

    Lists are mutable, ordered sequences — Python's workhorse collection.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Lists are mutable, ordered sequences — Python's workhorse collection. Backed by a dynamic array; O(1) index, O(n) insert/remove in middle.

    Understanding the topic

    Core concepts to understand:

    • Create: [1, 2, 3] or list().
    • Append O(1), pop end O(1), insert middle O(n).
    • Slicing creates a copy: xs[1:3].
    • Sort in place: xs.sort(); new list: sorted(xs).

    Syntax reference

    Visual flow / code:

    python
    xs = [3, 1, 4, 1, 5, 9]
    xs.append(2) # [3,1,4,1,5,9,2]
    xs.insert(0, 0) # [0,3,1,4,1,5,9,2]
    xs.remove(1) # removes first 1
    xs.pop() # removes & returns last
    xs.sort() # in place
    top3 = sorted(xs)[-3:]
    copy = xs[:] # full copy

    Execution workflow

    1Lists Workflow
    1 / 4

    Step 1

    Create: [1, 2, 3] or list().

    Apply this step while implementing lists in real code.

    Real-world use

    Lists are the default container for ordered data — request bodies, query results, batches. Use collections.deque if you need fast O(1) prepend.

    Best practices

    • Use append, not +=, in loops.
    • Slice to copy — never share mutable lists by reference accidentally.

    Common mistakes

    • a = b = [] makes both names point to the same list.

    Hands-on exercise

    Interview preparation — practice these questions:

    • List vs tuple?
    • When use deque?
    • Big-O of list ops?

    Summary

    In summary: Lists = mutable ordered. Append O(1), middle insert O(n).

    Ready to mark this lesson complete?Track your journey across the entire course.