Python Collections: Lists, Tuples, Dictionaries & Sets
GATE 2027 DA core
“Choose a built-in collection by its operations, then trace aliasing and aggregation correctly.”
1.Prerequisites and collection choices
§ 1.1.Prerequisites: the Python value and loop ideas from the previous lesson. If needed, revisit [Python Fundamentals](/notes/data-structures/da-python-language-fundamentals). A quick self-check: can you name the list values visited by a loop, and explain what append changes?
A list stores an ordered, mutable sequence; a tuple is an ordered, immutable sequence; a set stores unique hashable values; a dict maps unique hashable keys to values. These are built-in containers, not the same thing as the abstract data structures a course may implement manually.
For frequency counting, a dictionary is often the direct tool. For stack behavior, use append and pop at the end of a list. For FIFO queue behavior, collections.deque supports efficient appends and pops at both ends; repeatedly removing index zero from a list shifts the remaining elements.
Worked example: count labels
labels = ['a', 'b', 'a', 'c', 'b', 'a'], construct the frequency dictionary using one pass.Set counts = {}.
For each label x, assign counts[x] = counts.get(x, 0) + 1. Missing keys start at zero.
After six visits, counts is {'a': 3, 'b': 2, 'c': 1}. Six expected-constant-time updates give O(n) expected work.
Collection checks
Which structure best represents unique course codes with fast membership checks?
After `a=[1,2]; b=a; b.append(3)`, what is `a`?
One-minute revision
“Use the simplest container whose operations fit the task.”
- •list append/pop at end: amortized O(1)
- •list search: O(n)
- •dict/set lookup: expected O(1), with collision assumptions
- •Aliasing mutable lists
- •list front deletion shifts elements
- •unhashable keys
- •
sortmutates;sortedreturns a new list
Primary Academic References & Exam Sources
Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.
Curated for clarity, rigorous numerical solving, and semester excellence:
Visual thinkers who want to see each algorithm step animated clearly.
GATE-targeted coverage with step-by-step GATE PYQ walkthroughs.