Python Lists Explained: Indexing, Slicing, Mutation and Worked Examples
Trace one Python list from indexing to length-changing mutation, then master aliases, shallow copies, methods, comprehensions, costs and common output traps.
KnowledgeGate Team
Exam prep & CS education

Python list questions become difficult when they combine negative indices, half-open slices, length-changing assignment, method returns and aliases. The challenge is tracking both values and object identity. Trace both values and object identity through methods, comprehensions, operation costs and common traps. KnowledgeGate offers 50+ Python Lists questions in its practice bank, with structured practice in Coding & DSA Courses for Placements.
Build the right mental model for a Python list
A list is an ordered, mutable sequence that can hold repeated values and different types. For items = [7, "pen", 7, True], len(items) == 4, items[0] == 7, and items[-1] is True. The value 7 occurs at indices 0 and 2.
Python variables refer to list objects. For values = [12, 7, 18, 7, 20, 9], valid positive indices are 0 to 5 and negative indices are -1 to -6, so values[-2] is 20. Accessing values[6] raises IndexError. List elements can be replaced in place, unlike string or tuple elements.
Work one list through indexing, slicing and length-changing mutation
Start with scores = [12, 7, 18, 7, 20, 9]. The slice picked = scores[1:5:2] visits indices 1 and 3, so picked == [7, 7]. Stop index 5 is excluded. The slice creates a new outer list without changing scores.
Now apply four statements in order:
Statement | Result or value |
|
|---|---|---|
| No value to display |
|
| No value to display |
|
|
|
|
|
|
|
Replacing 18, 7 with 30, 40, 50 grows the length from 6 to 7 and shifts later elements. Finally, len(scores) == 7 and sum(scores) == 211 because 12 + 10 + 30 + 40 + 50 + 9 + 60 = 211. Also, scores[2:6] == [30, 40, 50, 9], while scores[::-1] creates the new list [60, 9, 50, 40, 30, 10, 12] without changing scores.

Separate aliases, shallow copies and nested objects
Take a = [12, 10, 30, 40, 50, 9, 60], then run b = a and c = a[:]. Here b aliases the same list, while c is a shallow outer copy.
After b[0] = 99, both a and b are [99, 10, 30, 40, 50, 9, 60]; c remains [12, 10, 30, 40, 50, 9, 60]. Therefore, a is b is True, while a is c and a == c are False. Identity tests objects; equality tests values.
A shallow copy still shares inner objects. For grid = [[1, 2], [3, 4]] and copy = grid[:], copy[0].append(5) makes both first rows [1, 2, 5]. Then copy[1] = [8, 9] replaces only the copied outer slot. Now grid == [[1, 2, 5], [3, 4]], while copy == [[1, 2, 5], [8, 9]].
![Reference diagram of shallow-copy aliasing: grid and copy share [1,2,5] at index 0, while their index 1 inner lists are [3,4] and [8,9].](https://cdn.knowledgegate.ai/blog-assets/blog_asset_1784740761169_igxrci.jpg)
Predict method effects and return values before running code
Start with nums = [4, 1, 4]. Successive calls to append(9), extend([2, 6]) and insert(1, 8) produce [4, 8, 1, 4, 9, 2, 6]. Then remove(4) deletes only the first match, leaving [8, 1, 4, 9, 2, 6]. Finally, last = nums.pop() returns 6 and leaves [8, 1, 4, 9, 2].
Record both the changed object and the expression result.
append([2, 6]) adds one nested list; extend([2, 6]) adds two elements. Next, answer = nums.sort() changes nums to [1, 2, 4, 8, 9], but answer is None. In contrast, ordered = sorted(nums, reverse=True) creates [9, 8, 4, 2, 1] and leaves nums ascending. Build the rules from the traces above; then use Python Output-Based Questions: 13 Solved Snippets to drill rapid prediction across aliases, copies, slicing and mutable defaults. pop mutates but returns the removed element.
Read list comprehensions as a three-step pipeline
Read a comprehension as source -> filter -> expression. In squares = [n * n for n in range(1, 8) if n % 2 == 1], the source supplies 1 through 7, the filter keeps 1, 3, 5, 7, and the expression produces [1, 9, 25, 49].
For words = ["gate", "ai", "python"], the expression tagged = [f"{i}:{word.upper()}" for i, word in enumerate(words, start=1)] gives ["1:GATE", "2:AI", "3:PYTHON"].
Nested clauses follow written loop order. pairs = [(x, y) for x in [1, 2] for y in [10, 20]] produces [(1, 10), (1, 20), (2, 10), (2, 20)], like these loops:
pairs = []
for x in [1, 2]:
for y in [10, 20]:
pairs.append((x, y))Connect each operation to its list cost
Reading or replacing scores[3] takes constant time. Searching with 50 in scores may inspect all seven elements, so it is linear. The slice scores[2:6] copies four references, making its cost proportional to slice length. Appending is amortised constant-time; front insertion or deletion is linear; comparison sorting is on the order of n log n.
From [12, 10, 30, 40, 50, 9, 60], insert(0, 5) shifts all seven references right and produces [5, 12, 10, 30, 40, 50, 9, 60]; append(5) avoids that shift. For repeated front operations, use DSA Using Python to choose a structure matching the access pattern.
Fix the traps used in exam-style output questions
Check tempting answers against the actual result and the reason.
[0] * 3is[0, 0, 0], butrows = [[0] * 2] * 3shares one inner list. Afterrows[0][1] = 7, every row is[0, 7]. Use[[0] * 2 for _ in range(3)]instead.for x in [1, 2, 3]: x *= 10does not replace the elements. It only rebindsx. Use indices or a comprehension.Removing while iterating can skip values. In
data = [2, 2, 3], removing each encountered2leaves[2, 3]because the second2shifts past the iterator.On
[[1, 2], [3, 4]],matrix[0][1]is2, whilematrix[1][0]is3. The first index selects the inner list.[],[0]and[False]have truth valuesFalse,TrueandTrue. Only the empty list is false by emptiness.
For broader practice, study Python Interview Questions for Freshers 2026 With Answers.
Keep the short version, then rerun the trace
Lists are ordered and mutable. Indices select elements; half-open slices make a new outer list; slice assignment can change length. b = a aliases, while a[:] makes a shallow outer copy. Predict mutation and return value separately.
For the complete sequence, continue with Python Course: Concepts, MCQs & Coding. Then rerun Section 2 with initial list [12, 7, 18, 7, 20, 11]. Recompute every state, popped value, reverse slice and sum. The final list is [12, 10, 30, 40, 50, 11, 60], with checksum 213.
Keep learning
Python Output-Based Questions: 13 Solved Snippets
Trace 13 compact Python snippets on references, slices and mutation. Each example gives the output first and then explains the object-level reason.
Strings in Python: Indexing, Slicing, Methods and Worked Examples
Build a reliable mental model for Python strings, then practise it through exact outputs, common traps and a complete record-cleaning program.
Python String Methods and Formatting: Immutability, Slicing and the Interview Question Patterns
Predict string output by tracking returned values, half-open slices and formatting specifications, then avoid the replace, find, identity and concatenation traps.
Python String MCQs: 12 Solved Questions on Slicing, Search and Methods
Solve 12 published Python string questions step by step. Trace indices, slices, return types and method contracts before checking each answer.





