datarekha

Lists, Tuples, Dicts, Sets & Gotchas

The four built-in collections and the gotchas GATE loves: append vs extend vs +, and aliasing vs copying a list.

8 min read Intermediate GATE DA Lesson 48 of 122

What you'll learn

  • Lists are mutable and ordered; tuples are immutable; dicts map keys to values; sets hold unique unordered items
  • append adds ONE element (even a whole list); extend adds each element; + builds a new list
  • Aliasing (b = a) shares one object; copying (b = a[:]) makes an independent list
  • Predicting list state after a real 2025 append/extend output question

Before you start

Python gives you four built-in collections, and GATE DA leans hardest on two behaviours that look harmless but flip an output: the difference between append, extend, and +, and the difference between aliasing a list and copying it. Get those two right and most collection output questions fall. The aliasing trap is no exam artefact either — “I changed one list and another one mutated too” is one of the most common real bugs in day-to-day data-wrangling code.

The four collections

list [1, 2, 3]mutable, ordered, indexablecan grow, shrink, reassign itemstuple (1, 2, 3)immutable, ordered, indexablefixed once created; hashabledict {‘a’: 1}key → value mappingkeys unique; lookup by keyset {1, 2, 3}unique, unorderedduplicates collapse; no indexing
Mutability and ordering are the two axes that decide each type’s behaviour.
  • List [1, 2, 3] — mutable, ordered, indexable. The workhorse.
  • Tuple (1, 2, 3) — like a list but immutable: you cannot change it after creation. Because it is fixed, it can be a dict key.
  • Dict `{'a': 1, 'b': 2}` — maps unique keys to values; you look items up by key. Assigning an existing key overwrites it (so len does not grow).
  • Set `{1, 2, 3}` — an unordered collection of unique items. Duplicates collapse, so `{1, 2, 2, 3}` has size 3.

append vs extend vs + — one element, or each element?

This is the single most-tested list distinction. Given a list A and another list B:

A = [1, 2, 3] B = [4, 5, 6]A.append(B)[1, 2, 3, [4,5,6]]B added as ONEnested elementlen = 4A.extend(B)[1, 2, 3, 4, 5, 6]EACH element ofB added in turnlen = 6A + B[1, 2, 3, 4, 5, 6]NEW list returned;A is unchangedA still len 3append and extend mutate A in place; + leaves A alone and hands back a new list.
Same two lists, three different results — the distinction GATE 2025 tested directly.
  • A.append(x) adds x as one element — even if x is itself a list, it goes in nested. Length grows by exactly 1.
  • A.extend(B) adds each element of B one by one. Length grows by len(B).
  • A + B builds and returns a new list; it does not change A (you must write A = A + B to keep it).

append and extend mutate the list in place and return None; + returns a fresh list. Run all three side by side:

Aliasing vs copying

Assigning a list with = does not copy it — both names point at the same object. Mutating through one name is visible through the other.

To get an independent copy use a[:], list(a), or a.copy().

How GATE asks this

A pure predict-the-output MCQ: a snippet builds a list, calls append/extend or aliases it, and you pick the final value (or its length) from four options. The distractors are exactly the other operations’ results — so the question is testing whether you know append nests, extend flattens, + is non-mutating, and = aliases. This appeared in GATE DA 2025.

Worked example — a real 2025 question

Start with A = [1, 2, 3] and B = [4, 5, 6]. Which single operation makes A equal to [1, 2, 3, 4, 5, 6]?

Check each:

  • A.append(B)A becomes [1, 2, 3, [4, 5, 6]]. B is added as one element, producing a nested list of length 4. Not equal.
  • A.extend(B)A becomes [1, 2, 3, 4, 5, 6]. Each of 4, 5, 6 is appended in turn, length 6. This is the answer.
  • A + B → evaluates to [1, 2, 3, 4, 5, 6] but does not change A; A is still [1, 2, 3] afterward (unless you reassign with A = A + B).

So only A.extend(B) mutates A into [1, 2, 3, 4, 5, 6]. This is GATE DA 2025’s list-operation question.

Quick check

Quick check

0/7
Q1A = [1, 2, 3]; B = [4, 5, 6]; A.append(B). What is len(A) afterward? (integer)numerical answer — type a number
Q2s = {1, 2, 2, 3, 3, 3}. What is len(s)? (integer)numerical answer — type a number
Q3a = [1, 2, 3]; b = a; b.append(4); print(a). What is printed?
Q4Which operations leave the original list A = [1, 2, 3] UNCHANGED? (select all that apply)select all that apply
Q5Which statements about Python collections are TRUE? (select all that apply)select all that apply
Q6A = [1, 2, 3]; B = [4, 5, 6]; C = A + B. What are A and C?
Q7def tag(item, basket): basket.append(item); return basket. You run cart = ['milk']; tag('eggs', cart). What does cart hold afterward?

Practice this in an interview

All questions
How do list, dict, and set comprehensions work in Python, and when should you avoid them?

Comprehensions are syntactic sugar for building a new collection by iterating over an iterable and optionally filtering elements. They are faster than equivalent for-loops because the iteration runs at the C level inside the interpreter. Avoid them when the expression is too complex to read at a glance — a plain loop with descriptive variable names is preferable.

Given a new data problem, how do you decide whether to use a list, dict, or set?

Choose a list when order matters and you need indexed access or duplicates. Choose a dict when you need to map keys to values and look up by key in O(1). Choose a set when you need uniqueness, fast membership testing, or set-algebra operations. Getting this choice wrong usually means either incorrect results (keeping duplicates when you needed uniqueness) or avoidable O(n) lookups.

What is the difference between a list and a tuple, and when should you use each?

Lists are mutable sequences; tuples are immutable. Use a tuple when the collection of items is fixed by meaning — coordinates, RGB values, function return values — and a list when the collection will grow, shrink, or be modified in place. Immutability also makes tuples hashable, so they can serve as dict keys or set members.

What set operations does Python support, and where are they practically useful in data work?

Python sets support union, intersection, difference, and symmetric difference as both operators and methods, all running in O(min(m,n)) to O(m+n) time. They are useful for deduplication, membership testing in large collections, and computing overlaps between datasets — operations that would be expensive with lists.

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Explore further

Related lessons

Skip to content