Lists in Python Formulas & Cheat Sheet
Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.
Core Concepts & Theoretical Foundations
- List Definition: An ordered, mutable, heterogeneous sequence of elements enclosed in square brackets ([...]).
- List Mutability: Unlike strings and tuples, lists are mutable. Individual items can be modified, replaced, added, or deleted in-place: lst[0] = 99.
- Indexing & Slicing: Supports two-way indexing (0 to n-1 and -1 to -n) and slicing lst[start : stop : step]. Modifying slices: lst[1:3] = [10, 20] replaces multiple elements in-place.
- List Operations: Concatenation (+ combines lists); Repetition (* replicates elements); Membership (in, not in); Traversal (for item in lst:).
- Adding Elements: .append(x) (adds single element x at the end); .extend(iterable) (appends all elements of iterable to the end); .insert(i, x) (inserts element x at index i).
- Removing Elements: .pop() (removes and returns last element); .pop(i) (removes and returns element at index i); .remove(x) (removes first occurrence of value x; raises ValueError if not found); .clear() (removes all elements); del lst[i] (deletes item or slice).
- Built-in List Methods: len(L), L.count(x), L.index(x), L.sort() (sorts in-place, returns None), L.sort(reverse=True), sorted(L) (returns new sorted list, leaves original unchanged), L.reverse() (reverses in-place), min(L), max(L), sum(L).
- Aliasing vs Cloning: L2 = L1 creates an alias (both variables reference the same list object). L2 = L1.copy() or L2 = L1[:] creates a shallow copy (independent list object).
Chapter Formulas & Equations (10 entries)
| # | Topic / Formula Name | Mathematical Form & Equation | Conditions, Meaning & Board Tips |
|---|---|---|---|
| 1 | List Append vs Extend Behavior |
|
.append() adds its argument as a single object, while .extend() iterates over the collection and adds each element individually. |
| 2 | Aliasing vs Shallow Copy Identity |
|
Modifying an aliased list changes both variables; modifying a cloned slice leaves the original list unaffected. |
| 3 | In-Place Sort vs Global sorted() |
|
Common bug: assigning L = L.sort() overwrites L with None! |
| 4 | List Pop vs Remove Distinction |
|
.pop() works with index positions (defaults to last element); .remove() searches for matching values. |
| 5 | Linear Search Algorithm Complexity |
|
Standard search algorithm across unsorted lists; time taken grows linearly with list length. |
| 6 | Nested List 2D Matrix Indexing |
|
Lists of lists represent 2D matrices, tables, and grids in Python algorithms. |
| 7 | List Slice Replacement Equation |
|
Replacing a slice with an iterable of different length changes the total size of the list in-place. |
| 8 | List Comprehension Mathematical Syntax |
|
Compact, elegant, and computationally efficient Python construct mirroring mathematical set-builder notation. |
| 9 | Binary Search Prerequisite & Complexity |
|
Significantly faster than linear search for large datasets; checks middle element and discards half the list. |
| 10 | Sum and Average of Numeric List |
|
Standard statistical calculation across lists of integers or floating-point values. |
CBSE Board Exam Tips & Frequent Pitfalls
- Never assign L = L.sort()! .sort() modifies the list in-place and returns None, so L will become None.
- Remember that L.append([3, 4]) adds the list as a single element [..., [3, 4]], whereas L.extend([3, 4]) adds 3 and 4 as individual elements.
- When removing items from a list in a loop, iterate over a copy L[:], otherwise skipping elements occurs due to shifting indices.
Step-by-Step Worked Board Exam Examples
Example 1: What will be the output of the following code? a = [10, 20, 30] b = a c = a.copy() a.append(40) b[0] = 99 print('a:', a) print('b:', b) print('c:', c)
b = a creates an alias pointing to the same memory object.
c = a.copy() creates an independent clone containing [10, 20, 30].
a.append(40) mutates the shared list to [10, 20, 30, 40].
b[0] = 99 mutates the first element of the shared list to 99.
Since a and b reference the same object: a is [99, 20, 30, 40], b is [99, 20, 30, 40].
c was cloned before any modifications, so c remains [10, 20, 30].
Example 2: Given L = [12, 18, 25, 30, 35], write a single-line list comprehension to extract all numbers divisible by 3.
Use list comprehension syntax: [x for x in iterable if condition].
Condition for divisibility by 3: x % 3 == 0.
Expression: div_by_3 = [x for x in L if x % 3 == 0].
Evaluation: 12 (yes), 18 (yes), 25 (no), 30 (yes), 35 (no) -> [12, 18, 30].
Common Mistakes to Avoid in Exam
- Calling .remove(x) when element x is not in the list raises a ValueError. Always check if x in L: first.
- Confusing sort() and reverse(). sort() orders items by magnitude, while reverse() simply flips the existing order backwards.
- Thinking L * 3 creates deep copies of inner lists. If L = [[0]], then L * 3 creates three references to the exact same inner list.
Target Practice Questions
Q1. What will len([1, [2, 3], 4]) return?
Correct Answer: Returns 3 (the nested list [2, 3] is counted as 1 element).
Q2. What is the output of [1, 2] + [3, 4] in Python?
Correct Answer: [1, 2, 3, 4] (concatenation).
Q3. How do you reverse a list L in-place?
Correct Answer: Using L.reverse().
Frequently Asked Questions
Q. What is the difference between append() and extend()?
append() adds its argument as a single element at the end of the list; extend() iterates over an iterable and appends each element individually.
Q. What is the time complexity of searching an element in an unsorted list?
Linear search has a time complexity of O(N), where N is the number of elements in the list.
Q. How do you delete an element at index 2 from list L?
Using del L[2] or L.pop(2).
Q. What does L.index(x) return?
Returns the index of the first occurrence of item x in the list, or raises a ValueError if x is not found.