[ ], separated by commas. Unlike strings, a list can hold items of mixed types — integers, floats, strings, Booleans, even other lists — all in one container. And because lists are mutable, you can add, remove, reorder, or replace items in place. Lists are the everyday workhorse of Python: marks of a class, items in a shopping cart, rows of a CSV file, pixels of an image — all natural lists.
- Ordered — every item has a fixed position (index), starting at 0.
- Mutable — items can be changed, added, or removed after creation.
- Heterogeneous — items need not be of the same type:
[1, "two", 3.0, True]. - Dynamic size — Python grows or shrinks the list automatically.
- Allows duplicates —
[1, 1, 2, 1]is valid.
17.1 Creating a List
The simplest way is to write a comma-separated list of values inside square brackets.
empty = [] # empty list — length 0 marks = [85, 72, 91, 68, 77] # 5 integers names = ["Aarav", "Kavya", "Ishan"] # 3 strings mixed = [1, "two", 3.0, True, [4, 5]] # any types, even a nested list single = [42] # one-element list print(type(marks)) # <class 'list'>
The list() constructor converts any iterable to a list:
print(list("Python")) # ['P', 'y', 't', 'h', 'o', 'n'] print(list(range(1, 6))) # [1, 2, 3, 4, 5] print(list()) # [] — empty list
17.2 Indexing — Accessing an Element
Like strings, lists support both positive (from the left, 0-based) and negative (from the right, −1 is last) indices.
marks = [85, 72, 91, 68, 77] print(marks[0]) # 85 — first print(marks[-1]) # 77 — last print(marks[2]) # 91 # print(marks[10]) ❌ IndexError: list index out of range
17.3 List Operations
Every sequence operation you already know from strings works on lists.
17.3.1 Concatenation +
a = [1, 2, 3] b = [4, 5] print(a + b) # [1, 2, 3, 4, 5]
17.3.2 Repetition *
print([0] * 5) # [0, 0, 0, 0, 0] — great for "fill-with-zeros" print(["ab", "cd"] * 2) # ['ab', 'cd', 'ab', 'cd']
17.3.3 Membership in / not in
marks = [85, 72, 91, 68] print(91 in marks) # True print(100 not in marks) # True
17.3.4 Slicing [start:stop:step]
Slicing returns a new list. The same rules as strings and range(): stop is excluded; defaults are start 0, stop len(), step 1.
nums = [10, 20, 30, 40, 50, 60] print(nums[1:4]) # [20, 30, 40] print(nums[:3]) # [10, 20, 30] print(nums[3:]) # [40, 50, 60] print(nums[::2]) # [10, 30, 50] — every 2nd print(nums[::-1]) # [60, 50, 40, 30, 20, 10] — reverse print(nums[-2:]) # [50, 60] — last two
17.3.5 Length — len()
print(len([10, 20, 30])) # 3 print(len([])) # 0
17.4 Lists Are Mutable
Unlike strings, lists let you modify an item by assignment, delete items, and change the list's length in place.
marks = [85, 72, 91, 68] marks[0] = 90 # replace the first mark print(marks) # [90, 72, 91, 68] marks[1:3] = [75, 80, 85] # slice-replace — length changes print(marks) # [90, 75, 80, 85, 68] del marks[0] # remove element at index 0 print(marks) # [75, 80, 85, 68]
b = a on a list does not copy — both names refer to the same list, and modifying one is visible through the other. Use b = a.copy() or b = a[:] to get an independent copy.
a = [1, 2, 3] b = a b.append(99) print(a) # [1, 2, 3, 99] — surprise! c = a.copy() # or a[:] c.append(42) print(a) # unchanged
17.5 Traversing a List
marks = [85, 72, 91, 68] # (1) direct — best when only values are needed for m in marks: print(m, end=" ") # 85 72 91 68 # (2) by index — best when you also need to write back for i in range(len(marks)): marks[i] += 5 # grace marks print(marks) # [90, 77, 96, 73] # (3) index + value together for i, m in enumerate(marks): print(i, "→", m) # (4) while-loop form i = 0 while i < len(marks): print(marks[i]) i += 1
17.6 Built-in FUNCTIONS that work on Lists
These are ordinary functions called as fn(list) — not methods.
| Function | What it does | Example (L = [3, 1, 4, 1, 5]) |
|---|---|---|
len(L) | Number of items | 5 |
min(L) | Smallest item | 1 |
max(L) | Largest item | 5 |
sum(L) | Sum of all numeric items | 14 |
list(iterable) | Build a new list from any iterable | list("abc") → ['a','b','c'] |
sorted(L) | Return a new sorted list; L itself is unchanged | [1, 1, 3, 4, 5] |
reversed(L) | Iterator over items in reverse | list(reversed(L)) → [5,1,4,1,3] |
marks = [85, 72, 91, 68, 77] print(len(marks)) # 5 print(max(marks)) # 91 print(min(marks)) # 68 print(sum(marks)) # 393 print(sum(marks) / len(marks)) # 78.6 — mean print(sorted(marks)) # [68, 72, 77, 85, 91] — NEW list, marks unchanged
17.7 Built-in METHODS on Lists
17.7.1 Adding items — append, extend, insert
| Method | Effect | Example | Result |
|---|---|---|---|
append(x) | Add x as ONE new element at the end | L.append(99) | [1,2,99] |
extend(iterable) | Add EVERY item of iterable at the end | L.extend([8,9]) | [1,2,8,9] |
insert(i, x) | Insert x before index i; existing items shift right | L.insert(1, 50) | [1,50,2] |
append vs extend — the classic gotcha:
a = [1, 2] a.append([3, 4]) print(a) # [1, 2, [3, 4]] — one element, itself a list b = [1, 2] b.extend([3, 4]) print(b) # [1, 2, 3, 4] — items "unpacked" and added individually
17.7.2 Removing items — remove, pop, clear
| Method | Effect | Example (L = [3, 1, 4, 1]) |
|---|---|---|
remove(x) | Delete the first occurrence of x. Error if x not found. | L.remove(1) → [3, 4, 1] |
pop(i) | Remove and return the item at index i. Default i = −1 (last item). | L.pop() → returns 1; L → [3, 1, 4] |
clear() | Empty the list in place | L.clear() → [] |
del L[i] | Statement (not a method) — delete item at index i, or a slice | del L[0], del L[1:3] |
17.7.3 Querying items — count and index
| Method | Returns | Example (L = [3, 1, 4, 1, 5]) |
|---|---|---|
count(x) | Number of times x appears | L.count(1) → 2; L.count(9) → 0 |
index(x) | Index of the first occurrence. Raises ValueError if not found. | L.index(4) → 2 |
17.7.4 Ordering — sort, reverse, sorted
| Call | Modifies list? | Returns | Example input [3, 1, 4, 1, 5] |
|---|---|---|---|
L.sort() | Yes — in place | None | L becomes [1, 1, 3, 4, 5] |
L.sort(reverse=True) | Yes | None | [5, 4, 3, 1, 1] |
L.reverse() | Yes | None | [5, 1, 4, 1, 3] |
sorted(L) | No — builds a NEW list | new list | [1, 1, 3, 4, 5]; L unchanged |
L[::-1] | No — slice builds a NEW list | new list | [5, 1, 4, 1, 3] |
L = [3, 1, 4] L = L.sort() # ❌ L is now None — .sort() returns None print(L) # NoneEither write
L.sort() on its own line, or use L = sorted(L) — but never both.
17.7.5 Other handy methods — copy
a = [1, 2, 3] b = a.copy() # independent copy b.append(99) print(a) # [1, 2, 3] — unchanged print(b) # [1, 2, 3, 99]
17.7.6 One-page cheat sheet
| Goal | Code |
|---|---|
| Empty list | L = [] |
| Fill with n zeros | L = [0] * n |
| 1 to n | L = list(range(1, n+1)) |
| Add at the end | L.append(x) |
| Merge another list in | L.extend(other) or L += other |
| Insert at a position | L.insert(i, x) |
| Remove by value | L.remove(x) |
| Remove by position | x = L.pop(i) (default last) |
| Sort in place | L.sort() |
| Get a sorted copy | L2 = sorted(L) |
| Reverse in place | L.reverse() |
| Reverse copy (slice) | L2 = L[::-1] |
| Independent copy | L2 = L.copy() or L[:] |
| Quick sum / mean / max / min | sum(L), sum(L)/len(L), max(L), min(L) |
17.8 Nested Lists
A nested list is a list whose items are themselves lists — the natural way to represent a grid, table or matrix.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[0]) # [1, 2, 3] — row 0
print(matrix[1][2]) # 6 — row 1 column 2
print(len(matrix)) # 3 — number of rows
print(len(matrix[0])) # 3 — number of columns
Two indices in a row give a single cell: matrix[row][col]. To visit every cell use nested loops:
students = [
["Aarav", 85, 72, 91],
["Kavya", 90, 88, 94],
["Ishan", 68, 75, 80]
]
for row in students:
name = row[0]
total = sum(row[1:])
print(f"{name}: total = {total}")
Output:
Aarav: total = 248 Kavya: total = 272 Ishan: total = 223
17.9 Worked Programs
17.9.1 Maximum, Minimum, Mean of a list
nums = [23, 45, 11, 78, 56, 34] # maximum largest = nums[0] for x in nums[1:]: if x > largest: largest = x # minimum smallest = nums[0] for x in nums[1:]: if x < smallest: smallest = x # mean total = 0 for x in nums: total += x mean = total / len(nums) print("Max =", largest) print("Min =", smallest) print("Mean =", mean)Output:
Max = 78 Min = 11 Mean = 41.166666666666664Using built-ins (shorter):
print(max(nums), min(nums), sum(nums)/len(nums))
17.9.2 Linear search
Scan the list one element at a time, stopping as soon as the target is found. The natural use-case for break — or for-else.
nums = [23, 45, 11, 78, 56, 34] target = int(input("Search for: ")) for i in range(len(nums)): if nums[i] == target: print(target, "found at index", i) break else: # for-else: runs only when no break print(target, "is not in the list")Sample runs:
Search for: 78 → 78 found at index 3 Search for: 99 → 99 is not in the list
17.9.3 Frequency of each element
Count how many times each unique value appears in a list. Two equivalent approaches:
count() with set to find uniques:
nums = [1, 2, 3, 2, 1, 3, 1, 4] for x in set(nums): # set removes duplicates print(x, "→", nums.count(x))Output:
1 → 3 2 → 2 3 → 2 4 → 1
nums = [1, 2, 3, 2, 1, 3, 1, 4] freq = {} for x in nums: if x in freq: freq[x] += 1 else: freq[x] = 1 print(freq) # {1: 3, 2: 2, 3: 2, 4: 1}
17.9.4 Bonus — Swap even-position and odd-position elements
One of the CBSE suggested practicals: swap elements at even indices with the ones at odd indices.
L = [10, 20, 30, 40, 50, 60] for i in range(0, len(L) - 1, 2): L[i], L[i+1] = L[i+1], L[i] print(L) # [20, 10, 40, 30, 60, 50]
17.10 List vs String — Side-by-Side
| Property | String | List |
|---|---|---|
| Brackets | " " / ' ' | [ ] |
| Ordered & indexable | Yes | Yes |
| Mutable | No | Yes |
| Element types | characters only | any type, can be mixed |
| Length can change? | No (new string each time) | Yes (in place) |
| Can be a dict key? | Yes — immutable | No — mutable |
| Common methods unique to it | upper, split, strip | append, pop, sort |
📌 Quick Revision — Chapter 17 at a Glance
- List = ordered, mutable, dynamic-sized sequence of any types. Created with
[ ]. list(iterable)converts any iterable (string, range, tuple…) into a list.- Indexing:
L[0]first ·L[-1]last. Out of range →IndexError. - Operations:
+concat ·*repeat ·inmembership ·len(L)length · slicingL[a:b:s]. - Mutability:
L[i] = x, slice-assign, anddelall modify the list in place.b = acreates an alias, not a copy — usea.copy()ora[:]. - Traversal:
for x in L,for i in range(len(L)),for i, x in enumerate(L),while. - Built-in FUNCTIONS:
len, min, max, sum, list, sorted, reversed. - Methods — Add:
append(x)(single),extend(iter)(each item),insert(i, x). - Methods — Remove:
remove(x)(by value),pop(i)(by index, returns it),clear(),del L[i]. - Methods — Query:
count(x),index(x). - Methods — Order:
sort()(in place, returnsNone),reverse()(in place),copy(). - Nested lists represent tables / matrices — access with two indices:
matrix[row][col]. - CBSE programs: max/min/mean with a single-pass loop; linear search with
for-else; frequency counting withcount()onset(L)or with a dictionary. - Biggest gotcha:
L = L.sort()setsLtoNone. UseL.sort()alone, orL = sorted(L).