Python Data Structures: Lists, Sets and Dictionaries Notes for GATE DA: Concepts, Formulas, Worked Examples & Practice

    Python Data Structures: Lists, Sets and Dictionaries notes for GATE DA: 12 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Chapter Roadmap: Lists and Sets for Code Tracing

    Chapter Roadmap: Lists and Sets for Code Tracing

    Orientation Level 1 60 sec Importance 0.58 Toughness 0.20

    This chapter is about predicting the final state of Python data structures after a sequence of operations.

    Topic 1: Python List Operations and In-Place Updates

    Focus on:

    • Lists as ordered, mutable sequences.
    • Indexing and slicing.
    • append, extend, insert, pop, remove, sort, reverse.
    • Concatenation with +.
    • Augmented assignment with +=.
    • Aliasing: when two names refer to the same list.
    • Copying: when a separate list is created.

    Topic 2: Python Set Operations and Membership Tracing

    Focus on:

    • Sets as unordered collections of unique elements.
    • Membership testing using in.
    • Union, intersection, difference, and symmetric difference.
    • Mutating set operations.
    • Tracing loops that repeatedly update sets.
    • Simultaneous assignment such as A, B = A - B, B - A.

    Core Question to Keep Asking

    1. Is the same object being changed?
    2. Is a new object being created?
    3. Which variable names now point to which object?
    4. What will the next loop condition or membership test see?

    If you can answer these four questions, most list and set tracing problems become mechanical.

    Lists Are Ordered, Mutable Boxes

    Lists Are Ordered, Mutable Boxes

    Concept Level 2 75 sec Importance 0.60 Toughness 0.25

    A Python list is an ordered collection. The position of each item matters, and the list can be changed after creation.

    items = [10, 20, 30]

    Basic Properties

    PropertyMeaning
    Ordered[1, 2, 3] is different from [3, 2, 1]
    MutableElements can be changed, added, or removed
    Duplicates allowed[1, 1, 2] is valid
    Indexed by positionFirst item is index 0, last is index -1

    Common Operations

    x = [10, 20, 30]
    x[0] = 5          # replace item
    x.append(40)      # add one item at end
    x.insert(1, 15)   # insert at index 1
    x.pop()           # remove and return last item
    x.remove(20)      # remove first occurrence of 20
    n = len(x)        # length

    Simple Trace

    x = [10, 20, 30]
    x[0] = 5
    x.append(40)
    x.insert(1, 15)
    StepValue of x
    Start[10, 20, 30]
    x[0] = 5[5, 20, 30]
    x.append(40)[5, 20, 30, 40]
    x.insert(1, 15)[5, 15, 20, 30, 40]

    The key idea is that the list changes in place. No new list is created by these operations.

    Combining Lists: append, extend, plus, and plus-equals

    Combining Lists: append, extend, plus, and plus-equals

    Method Level 3 90 sec Importance 0.62 Toughness 0.45

    Suppose:

    A = [1, 2, 3]
    B = [4, 5, 6]

    The goal is often to get [1, 2, 3, 4, 5, 6], but different operations behave differently.

    OperationResulting Value of AEffect
    A.append(B)[1, 2, 3, [4, 5, 6]]Adds B as one object
    A.extend(B)[1, 2, 3, 4, 5, 6]Adds each item of B
    A = A + B[1, 2, 3, 4, 5, 6]Creates a new list and rebinds A
    A += B[1, 2, 3, 4, 5, 6]In-place extension for lists
    A[len(A):] = B[1, 2, 3, 4, 5, 6]Slice assignment, mutates A

    append puts the whole object inside

    A = [1, 2, 3]
    B = [4, 5, 6]
    A.append(B)

    Final A:

    [1, 2, 3, [4, 5, 6]]

    The fourth element is itself a list.

    extend adds the items one by one

    A = [1, 2, 3]
    B = [4, 5, 6]
    A.extend(B)

    Final A:

    [1, 2, 3, 4, 5, 6]

    Memory Hook

    • append: add one thing.
    • extend: add many things.
    • +: make a new list.
    • +=: usually change the existing list.

    Tracing List Updates with Aliasing and Copying

    Tracing List Updates with Aliasing and Copying

    Worked Example Level 3 110 sec Importance 0.62 Toughness 0.55
    a = [1, 2, 3]
    b = a
    c = a[:]
    a += [4]
    a = a + [5]
    b.append(6)
    print(a)
    print(b)
    print(c)
    Initial: a and b share one list; c gets a separate copy.
    After a += [4], the shared list becomes [1, 2, 3, 4].
    After a = a + [5], a moves to a new list.
    After b.append(6), b changes the old shared list.

    Step 1: Initial assignment

    a = [1, 2, 3]
    b = a
    c = a[:]
    NameMeaning
    apoints to original list [1, 2, 3]
    bpoints to the same original list
    cpoints to a separate copy [1, 2, 3]

    Steps 2 to 4

    StepCodeEffectResult
    2a += [4]Mutates the shared list[1, 2, 3, 4] seen by a and b
    3a = a + [5]Creates a new list and rebinds aa = [1, 2, 3, 4, 5]; b still points to the old list
    4b.append(6)Mutates the old shared listb = [1, 2, 3, 4, 6]

    Final Values

    a = [1, 2, 3, 4, 5]
    b = [1, 2, 3, 4, 6]
    c = [1, 2, 3]

    The main lesson: b = a creates another name for the same list, while c = a[:] creates a separate top-level copy.

    More notes in this unit

    chapter
    Python Data Structures: Lists, Sets and Dictionaries Notes for GATE DA: Concepts, Formulas, Worked Examples & Practice

    Python Data Structures: Lists, Sets and Dictionaries notes for GATE DA: 12 study cards covering concepts, formulas, shortcuts and exam traps, plus solved prac

    Free preview ends here

    Login to view the complete notes

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.