Creating lists and indexing
L = [4, 7, 2, 5], E = [], M = [1, 'two', 3.0, [4, 5]], list('abc') → ['a', 'b', 'c'].
Indexing: L[0] = 4, L[-1] = 5. Lists are mutable: L[1] = 9 → [4, 9, 2, 5]. An index outside the list gives IndexError.
List operations
- Concatenation: [1, 2] + [3] → [1, 2, 3]
- Repetition: [0] * 4 → [0, 0, 0, 0]
- Membership: 7 in L → True
- Slicing: L[1:3] → [7, 2]; L[::-1] → reversed copy
- Comparison: [1, 2] == [1, 2] → True
Traversing a list
for x in L:
print(x)
for i in range(len(L)):
L[i] = L[i] * 2 # change using index
i = 0
while i < len(L):
print(L[i]); i += 1
List functions and methods
| Method | What it does (L = [4, 7, 2]) |
|---|---|
| len(L) | 3 |
| L.append(6) | adds one item at the end → [4, 7, 2, 6] |
| L.extend([8, 9]) | adds each item of another list |
| L.insert(1, 5) | puts 5 at index 1 |
| L.count(7) | how many times 7 appears |
| L.index(7) | first index of 7 (ValueError if missing) |
| L.remove(7) | deletes the first 7 |
| L.pop(i) / L.pop() | removes and returns item at i / the last item |
| L.reverse() | reverses in place |
| L.sort(), L.sort(reverse=True) | sorts in place |
| sorted(L) | returns a new sorted list |
| min(L), max(L), sum(L) | 2, 7, 13 |
del L[0] deletes by index; L.clear() empties the list.
Nested lists
A list can hold other lists: M = [[1, 2, 3], [4, 5, 6]]. M[1] is [4, 5, 6]; M[1][2] is 6. Traverse with nested loops:
for row in M:
for x in row:
print(x, end=' ')
print()
Programs: max, min, mean, linear search, frequency
L = [4, 7, 2, 9, 5]
big = L[0]; small = L[0]
for x in L:
if x > big: big = x
if x < small: small = x
mean = sum(L) / len(L) # 5.4
# linear search
key = 9; pos = -1
for i in range(len(L)):
if L[i] == key:
pos = i; break
print(pos) # 3
# frequency of each element
for x in set(L):
print(x, L.count(x))Linear search checks items one by one; in the worst case it looks at every item.
Key formulas and definitions
- L[i] (0-based), L[-1] last; lists are mutable
- append(x) one item · extend(list) many items · insert(i, x)
- remove(value) · pop(index) returns item
- sort() in place · sorted() new list
- mean = sum(L) / len(L)
Worked examples
1. L = [10, 20, 30, 40]. Find L[1:3], L[-1], L * 2, 25 in L.
[20, 30]; 40; [10, 20, 30, 40, 10, 20, 30, 40]; False.
2. Output? L = [3, 1, 2]; L.append([4, 5]); print(len(L))
4, because append adds the whole list as one item: [3, 1, 2, [4, 5]].
3. Output? L = [3, 1, 2]; L.extend([4, 5]); L.insert(0, 9); L.remove(1); print(L)
[9, 3, 2, 4, 5]
4. Find max, min and mean of [12, 5, 8, 20, 15] without max/min.
big = small = 12; after checking all: big = 20, small = 5; mean = 60 / 5 = 12.0.
5. Linear search for 15 in [12, 5, 8, 20, 15]. How many comparisons?
12, 5, 8, 20, 15 → found at index 4 after 5 comparisons.
6. Swap elements at even positions with the next odd position in [1, 2, 3, 4, 5, 6].
for i in range(0, len(L) - 1, 2): L[i], L[i+1] = L[i+1], L[i] # [2, 1, 4, 3, 6, 5]
7. Count the frequency of each element in [2, 3, 2, 5, 3, 2].
Using a loop and count(): 2 → 3, 3 → 2, 5 → 1.
Common mistakes
- Confusing append and extend: append([4, 5]) adds one nested list; extend adds 4 and 5 separately.
- Writing L = L.sort(). sort() returns None; just call L.sort().
- Using remove with an index. remove takes a value; pop takes an index.
- Changing a list while looping over it with for x in L, which skips items.