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.
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, 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 (solendoes 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.append(x)addsxas one element — even ifxis itself a list, it goes in nested. Length grows by exactly 1.A.extend(B)adds each element ofBone by one. Length grows bylen(B).A + Bbuilds and returns a new list; it does not changeA(you must writeA = A + Bto 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]andB = [4, 5, 6]. Which single operation makesAequal to[1, 2, 3, 4, 5, 6]?
Check each:
A.append(B)→Abecomes[1, 2, 3, [4, 5, 6]].Bis added as one element, producing a nested list of length 4. Not equal.A.extend(B)→Abecomes[1, 2, 3, 4, 5, 6]. Each of4, 5, 6is appended in turn, length 6. This is the answer.A + B→ evaluates to[1, 2, 3, 4, 5, 6]but does not changeA;Ais still[1, 2, 3]afterward (unless you reassign withA = 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
Practice this in an interview
All questionsComprehensions 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.
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.
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.
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.