List Processing and In-Place Mutation Notes for GATE DA: Concepts, Formulas, Worked Examples & Practice

    List Processing and In-Place Mutation notes for GATE DA: 11 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Chapter Roadmap: List Processing and In-Place Mutation

    Chapter Roadmap

    Chapter journey

    1. Recursive List Processing and In-Place Mutation
      Current topic. High-importance tracing.
      • Single-index recursive scans
      • Adjacent-swap counting
      • Two-pointer reversal
    2. Chapter mastery
      • Track exact list state after every swap
      • Decide if call is scan or reversal
      • Predict final return value and list
    Weight hint: Core tracing topic. Small code pieces can carry good marks through careful step-by-step execution.

    Topic Hero: Recursion Meets Mutable Lists

    Topic Hero: Recursion Meets Mutable Lists

    A Python list is mutable. If a recursive function changes the list, the changed list is visible to all deeper recursive calls.

    Two core shapes appear repeatedly:

    Shape Indices used Typical operation
    Single-index scan i Compare and possibly swap L[i] and L[i+1]
    Two-pointer reversal s1, s2 Swap D[s1] and D[s2], then move inward
    Key idea: The list is shared, but the index variables are local to each recursive call.

    The List Is Shared, Indices Are Local

    The List Is Shared, Indices Are Local

    In-place mutation

    def swap_positions(L, i, j):
    L[i], L[j] = L[j], L[i]

    This changes the actual list object. There is no new list being created.

    What this means for recursion

    • If call 1 swaps elements, call 2 sees the swapped list.
    • If call 2 swaps elements, call 3 sees the newer list.
    • Changing i, j, s1, or s2 inside one call does not change those variables in earlier calls.

    Python swap detail

    L[i], L[j] = L[j], L[i]

    The right-hand side is evaluated first using the old list values. Then both assignments happen together. This avoids the classic mistake of overwriting one value before saving the other.

    Single-Index Recursive Scan Pattern

    Single-Index Recursive Scan Pattern

    Basic shape

    def scan(L, i=0):
    if i >= len(L) - 1:
        return 0
    if L[i] > L[i + 1]:
        L[i], L[i + 1] = L[i + 1], L[i]
        return 1 + scan(L, i + 1)
    return scan(L, i + 1)

    Why the base condition is i >= len(L) - 1

    The function compares L[i] with L[i + 1].

    If len(L) = n, the last valid comparison is between indices n - 2 and n - 1. Therefore recursion should stop when i reaches n - 1.

    Empty and single-element lists

    • If len(L) = 0, then len(L) - 1 = -1, and 0 >= -1 is true.
    • If len(L) = 1, then len(L) - 1 = 0, and 0 >= 0 is true.

    Both cases safely return the base value.

    More notes in this unit

    chapter
    List Processing and In-Place Mutation Notes for GATE DA: Concepts, Formulas, Worked Examples & Practice

    List Processing and In-Place Mutation notes for GATE DA: 11 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    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.