chapter
    Python Data Structures: Lists, Sets and Dictionaries Short Notes for GATE DA

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

    python data structures lists sets and dictionaries short notes

    Final Revision: List and Set Tracing Rules

    Final Revision: List and Set Tracing Rules

    Summary Level 5 70 sec Importance 0.62 Toughness 0.25

    Lists

    RuleMeaning
    OrderedPosition matters
    MutableCan be changed in place
    appendAdds one object
    extendAdds items from an iterable
    +Creates a new list
    +=Mutates the existing list
    b = aAlias, same object
    b = a[:]Shallow copy, new outer list

    Sets

    RuleMeaning
    Unique elementsDuplicates are removed
    UnorderedDo not rely on position
    inMembership test
    A | BUnion
    A & BIntersection
    A - BElements in A but not B
    A ^ BElements in exactly one of them
    S |= TMutates S
    S = S | TRebinds S to a new set

    Tracing Method

    1. Write the current value of each variable.
    2. Decide whether the operation mutates or rebinds.
    3. For simultaneous assignment, evaluate all right-hand sides first.
    4. After a loop body, recheck the loop condition.

    Memory Hook

    Append one, extend many.
    Plus new, plus-equals changes.
    Sets deduplicate.
    Right side first.

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    Level 1: Warm-up

    Which of the following statements is true regarding list mutation and rebinding in Python?

    Question 2
    Level 1: Warm-up

    Given , how many of the following expressions evaluate to True?

    (i) 1 in S

    (ii) {1} in S

    (iii) {1, 2} <= S

    (iv) 4 not in S

    Question 3
    Level 1: Warm-up

    Which of the following CANNOT be an element of a Python set?

    Question 4
    Level 1: Warm-up

    Consider the following Python code:

    ```python

    x = [1, 2, 3]

    y = x

    z = x[:]

    ```

    What is the minimum number of distinct list objects present in memory after execution?

    Question 5
    Level 1: Warm-up

    For a Python list x, which of the following operations guarantees that the length of x increases by exactly 1, regardless of the contents of the added elements?

    Question 6
    Level 1: Warm-up

    Let A = {1, 2} and B = {2, 3}. After executing the simultaneous assignment A, B = B - A, A & B, what is the maximum element in the new set B?

    Question 7
    Level 1: Warm-up

    Consider the following Python code:

    ```python

    S = {1, 2, 3}

    S = S | {4}

    ```

    Assertion (A): After the above code executes, the original set object is mutated to include 4.

    Reason (R): The | operator creates a new set object, and the assignment operator = rebinds the name S to this new object.

    Question 8
    Level 1: Warm-up

    If set has 7 elements, set has 5 elements, and has 2 elements, what is the number of elements in ?

    Question 9
    Level 1: Warm-up

    Let and be sets such that , , and . What is ?

    Question 10
    Level 1: Warm-up

    Let and . Rank the following sets by size, from smallest to largest: , , .

    Free preview ends here

    Login to view the complete short 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.

    Python Data Structures: Lists, Sets and Dictionaries Short Notes for GATE DA

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

    Final Revision: List and Set Tracing Rules

    Final Revision: List and Set Tracing Rules

    Summary Level 5 70 sec Importance 0.62 Toughness 0.25

    Lists

    RuleMeaning
    OrderedPosition matters
    MutableCan be changed in place
    appendAdds one object
    extendAdds items from an iterable
    +Creates a new list
    +=Mutates the existing list
    b = aAlias, same object
    b = a[:]Shallow copy, new outer list

    Sets

    RuleMeaning
    Unique elementsDuplicates are removed
    UnorderedDo not rely on position
    inMembership test
    A | BUnion
    A & BIntersection
    A - BElements in A but not B
    A ^ BElements in exactly one of them
    S |= TMutates S
    S = S | TRebinds S to a new set

    Tracing Method

    1. Write the current value of each variable.
    2. Decide whether the operation mutates or rebinds.
    3. For simultaneous assignment, evaluate all right-hand sides first.
    4. After a loop body, recheck the loop condition.

    Memory Hook

    Append one, extend many.
    Plus new, plus-equals changes.
    Sets deduplicate.
    Right side first.

    Python Data Structures: Lists, Sets and Dictionaries: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Programming, Data Structures and Algorithms MCQ

    Which of the following statements is true regarding list mutation and rebinding in Python?

    1. A.

      x = x + [4] mutates the existing list in place.

    2. B.

      x += [4] creates a new list and leaves the original unchanged.

    3. C.

      y = x creates a separate copy of the list.

    4. D.

      x.append(4) modifies the existing list, so all aliases see the change.

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: This is a statement truth question testing the boundary between mutation and rebinding.

    Step 1: Analyze Option A

    x = x + [4] uses the + operator, which creates a new list and rebinds x. It does not mutate. (False)

    Step 2: Analyze Option B

    x += [4] uses augmented assignment. For lists, this mutates the existing list in place. It does not create a new list. (False)

    Step 3: Analyze Option C

    y = x creates an alias, not a copy. Both point to the same object. (False)

    Step 4: Analyze Option D

    x.append(4) is a mutating method. It changes the existing list, so any other variable pointing to the same list (aliases) will see the change. (True)

    Answer: Option D

    Question 2 · Programming, Data Structures and Algorithms MCQ

    Given , how many of the following expressions evaluate to True?

    (i) 1 in S

    (ii) {1} in S

    (iii) {1, 2} <= S

    (iv) 4 not in S

    1. A.

      1

    2. B.

      2

    3. C.

      3

    4. D.

      4

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: Distinguish between membership testing (in) and subset testing (<=). The in operator checks if an element is in the set, not if a set is in the set.

    Step 1: Evaluate each expression:

    (i) 1 in S: Is the integer 1 an element of S?

    S = {1, 2, 3}, so yes. True

    (ii) {1} in S: Is the set {1} an element of S?

    S contains integers 1, 2, 3, not the set {1}. False

    (iii) {1, 2} <= S: Is {1, 2} a subset of S?

    Both 1 and 2 are in S, so yes. True

    (iv) 4 not in S: Is 4 not an element of S?

    S = {1, 2, 3}, so 4 is not in S. True

    Step 2: Count the True expressions:

    (i) True, (ii) False, (iii) True, (iv) True

    Total: 3 expressions are True.

    Answer: C

    Common mistake: Thinking {1} in S is True because 1 is in S. But {1} is a set, not the integer 1.

    Question 3 · Programming, Data Structures and Algorithms MCQ

    Which of the following CANNOT be an element of a Python set?

    1. A.

      The integer 42

    2. B.

      The string "hello"

    3. C.

      The tuple (1, 2, 3)

    4. D.

      The list [1, 2, 3]

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: Set elements must be hashable (immutable). Lists are mutable and therefore not hashable.

    Step 1: Recall the requirement for set elements:

    • Elements must be hashable
    • Hashable objects are immutable

    Step 2: Check each option:

    • Integer 42: immutable → hashable → allowed
    • String "hello": immutable → hashable → allowed
    • Tuple (1, 2, 3): immutable → hashable → allowed (if all elements are hashable)
    • List [1, 2, 3]: mutable → not hashable → NOT allowed

    Step 3: Lists are mutable, so they cannot be set elements.

    Answer: D

    Common mistake: Thinking tuples are also invalid because they "look like" lists, but tuples are immutable and hashable.

    Question 4 · Programming, Data Structures and Algorithms MCQ

    Consider the following Python code:

    ```python

    x = [1, 2, 3]

    y = x

    z = x[:]

    ```

    What is the minimum number of distinct list objects present in memory after execution?

    1. A.

      1

    2. B.

      3

    3. C.

      4

    4. D.

      2

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: This is a contradiction question testing the difference between aliasing (same object) and slicing (new object).

    Step 1: Analyze x = [1, 2, 3]

    Creates 1 list object. x points to it.

    Step 2: Analyze y = x

    This is aliasing. y points to the same object as x. No new object is created.

    Step 3: Analyze z = x[:]

    This is a shallow copy using slicing. It creates 1 new list object. z points to it.

    Step 4: Count distinct objects

    Object 1: pointed to by x and y.

    Object 2: pointed to by z.

    Total = 2 distinct list objects.

    Answer: 2 (Option D)

    Question 5 · Programming, Data Structures and Algorithms MCQ

    For a Python list x, which of the following operations guarantees that the length of x increases by exactly 1, regardless of the contents of the added elements?

    1. A.

      x.append([1, 2])

    2. B.

      x.extend([1, 2])

    3. C.

      x += [1, 2]

    4. D.

      x = x + [1, 2]

    Correct Answer:

    A

    Step-by-Step Solution

    Insight: append adds its argument as a single element, while extend and + add the individual elements of the iterable.

    Exam route: x.append([1, 2]) adds the list [1, 2] as one single element. Length increases by 1. extend and + add 1 and 2 separately, increasing length by 2.

    Learning route: The append method takes its argument and inserts it as exactly one element at the end of the list, even if that argument is itself a list. Thus, x.append([1, 2]) increases the length of x by exactly 1. In contrast, extend and the + operator iterate over the argument and add each item individually, which would increase the length by 2 in this case.

    Wrong path: A student might ignore the constraint "increases by exactly 1" and simply look for an operation that adds elements, choosing extend or + (Options B, C, D). The break occurs at failing to distinguish between adding one object versus adding the contents of an object.

    Generalization: append always increases list length by 1. extend increases length by the number of elements in the iterable.

    Verification: x.append([1, 2]) results in x having one new element (the list [1, 2]). Length increases by 1. Correct.

    Question 6 · Programming, Data Structures and Algorithms MCQ

    Let A = {1, 2} and B = {2, 3}. After executing the simultaneous assignment A, B = B - A, A & B, what is the maximum element in the new set B?

    1. A.

      3

    2. B.

      1

    3. C.

      2

    4. D.

      0

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: In simultaneous assignment, all right-hand side expressions are evaluated using the original values of the sets before any assignments occur.

    Exam route: Original A = {1, 2}, B = {2, 3}. B - A = {3}. A & B = {2}. B is assigned {2}. The maximum element in B is 2.

    Learning route: The right-hand sides are evaluated first. B - A using the original sets is {2, 3} - {1, 2} = {3}. A & B using the original sets is {1, 2} & {2, 3} = {2}. Then, the assignments happen: A becomes {3} and B becomes {2}. The new set B is {2}, and its maximum element is 2.

    Wrong path: A student might evaluate sequentially. A becomes B - A = {3}. Then B becomes A & B = {3} & {2, 3} = {3}. The maximum element would be 3 (Option A). This breaks because simultaneous assignment does not update variables one by one; it uses the old values for all right-hand sides.

    Generalization: In x, y = expr1, expr2, both expr1 and expr2 are computed before x or y is updated.

    Verification: The original A and B yield {3} and {2}. B receives {2}, so the max is 2.

    Question 7 · Programming, Data Structures and Algorithms MCQ

    Consider the following Python code:

    ```python

    S = {1, 2, 3}

    S = S | {4}

    ```

    Assertion (A): After the above code executes, the original set object is mutated to include 4.

    Reason (R): The | operator creates a new set object, and the assignment operator = rebinds the name S to this new object.

    1. A.

      Both A and R are true and R is the correct explanation of A

    2. B.

      Both A and R are true but R is NOT the correct explanation of A

    3. C.

      A is true but R is false

    4. D.

      A is false but R is true

    Correct Answer:

    D

    Step-by-Step Solution

    Insight: The | operator on sets creates a new set, and = rebinds the variable name; it does not mutate the original set in place.

    Exam route: S = S | {4} evaluates S | {4} to a new set {1, 2, 3, 4} and rebinds S to it. The original set object {1, 2, 3} is unchanged. Thus, A is false. R correctly describes this rebinding behavior, so R is true. Answer is D.

    Learning route: The expression S | {4} computes the union, which produces a brand new set object in memory. The assignment S = ... then points the name S to this new object. The original set object that S previously pointed to is not modified; it remains {1, 2, 3} (though it may be garbage collected if no other names reference it). Therefore, Assertion A is false because no mutation occurs. Reason R is true because it accurately describes the creation of a new object and the rebinding of the name S.

    Wrong path: A student might commit a unit mismatch error, confusing the unit of change (variable rebinding) with object mutation. They might think S = S | {4} adds 4 to the existing set object, making A true. This leads to choosing Option A or C. The break occurs at misunderstanding that | does not have an in-place effect like |= does.

    Generalization: For sets, | creates a new object (rebinding), while |= mutates the existing object in place.

    Verification: If another variable T = S was set before the operation, T would remain {1, 2, 3}, proving the original object was not mutated.

    Question 8 · Programming, Data Structures and Algorithms MCQ

    If set has 7 elements, set has 5 elements, and has 2 elements, what is the number of elements in ?

    1. A.

      9

    2. B.

      10

    3. C.

      12

    4. D.

      14

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a direct application of the inclusion-exclusion principle for two sets.

    Step 1: Recall the formula for the size of a union:

    Step 2: Substitute the given values:

    Step 3: Calculate:

    Answer: 10

    Common mistake: Forgetting to subtract the intersection gives , which is option C. This overcounts the 2 elements that appear in both sets.

    Question 9 · Programming, Data Structures and Algorithms MCQ

    Let and be sets such that , , and . What is ?

    1. A.

      3

    2. B.

      5

    3. C.

      7

    4. D.

      21

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a reverse application of the inclusion-exclusion principle. Instead of finding the union from the intersection, we find the intersection from the union.

    Step 1: Recall the inclusion-exclusion formula:

    Step 2: Rearrange to solve for :

    Step 3: Substitute the given values:

    Step 4: Calculate:

    Answer: 5

    Common mistake: Using the wrong formula arrangement, such as (option A if taking absolute value), or (option C), or simply adding (option D).

    Question 10 · Programming, Data Structures and Algorithms MCQ

    Let and . Rank the following sets by size, from smallest to largest: , , .

    1. A.

      , ,

    2. B.

      , ,

    3. C.

      , ,

    4. D.

      , ,

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: Calculate the size of each set operation, then rank them.

    Step 1: Calculate (elements in both A and B):

    Size: 2

    Step 2: Calculate (elements in A but not in B):

    Size: 3

    Step 3: Calculate (elements in A or B or both):

    Size: 6

    Step 4: Rank by size (smallest to largest):

    , ,

    Ranking: , ,

    Answer: A

    More short notes in this unit