VM-LEARNING /class.xi ·track.cs ·ch-2-11 session: 2026_27
$cd ..

~/Lists

root@vm-learning ~ $ open ch-2-11
UNIT 2 ▪ CHAPTER 11
17
Lists
Creation · Indexing · Slicing · Methods · Nested lists · Worked Programs
A list is an ordered, mutable sequence of values written inside square brackets [ ], 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.
Key properties of a list:
  • 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.
Learning Outcome 1: Create lists and apply the basic sequence operations — concatenation, repetition, membership, length, indexing, slicing.

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.

Indexing a list: 85 72 91 68 77 0 1 2 3 4 ← positive index −5 −4 −3 −2 −1 ← negative index marks = [85, 72, 91, 68, 77] → marks[0] = 85, marks[−1] = 77
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]
Alias trap. Assignment 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
Learning Outcome 2: Traverse a list using for and while loops.

17.5 Traversing a List

Three common idioms:
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
Learning Outcome 3: Apply Python's built-in list functions and methods.

17.6 Built-in FUNCTIONS that work on Lists

These are ordinary functions called as fn(list)not methods.

FunctionWhat it doesExample (L = [3, 1, 4, 1, 5])
len(L)Number of items5
min(L)Smallest item1
max(L)Largest item5
sum(L)Sum of all numeric items14
list(iterable)Build a new list from any iterablelist("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 reverselist(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

The CBSE list-method families: List Methods (CBSE Class XI set) Add append(x) extend(iter) insert(i, x) in-place Remove remove(x) pop(i) clear() in-place Query count(x) index(x) returns info Order sort() reverse() copy() in-place (except copy) Built-in functions len · min · max sum · list sorted · reversed called as fn(L) Methods modify the list directly (in place). Functions usually return a new value.

17.7.1 Adding items — append, extend, insert

MethodEffectExampleResult
append(x)Add x as ONE new element at the endL.append(99)[1,2,99]
extend(iterable)Add EVERY item of iterable at the endL.extend([8,9])[1,2,8,9]
insert(i, x)Insert x before index i; existing items shift rightL.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

MethodEffectExample (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 placeL.clear()[]
del L[i]Statement (not a method) — delete item at index i, or a slicedel L[0], del L[1:3]

17.7.3 Querying items — count and index

MethodReturnsExample (L = [3, 1, 4, 1, 5])
count(x)Number of times x appearsL.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

CallModifies list?ReturnsExample input [3, 1, 4, 1, 5]
L.sort()Yes — in placeNoneL becomes [1, 1, 3, 4, 5]
L.sort(reverse=True)YesNone[5, 4, 3, 1, 1]
L.reverse()YesNone[5, 1, 4, 1, 3]
sorted(L)No — builds a NEW listnew list[1, 1, 3, 4, 5]; L unchanged
L[::-1]No — slice builds a NEW listnew list[5, 1, 4, 1, 3]
A common beginner bug:
L = [3, 1, 4]
L = L.sort()         # ❌ L is now None — .sort() returns None
print(L)             # None
Either 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

GoalCode
Empty listL = []
Fill with n zerosL = [0] * n
1 to nL = list(range(1, n+1))
Add at the endL.append(x)
Merge another list inL.extend(other) or L += other
Insert at a positionL.insert(i, x)
Remove by valueL.remove(x)
Remove by positionx = L.pop(i) (default last)
Sort in placeL.sort()
Get a sorted copyL2 = sorted(L)
Reverse in placeL.reverse()
Reverse copy (slice)L2 = L[::-1]
Independent copyL2 = L.copy()  or  L[:]
Quick sum / mean / max / minsum(L), sum(L)/len(L), max(L), min(L)
Learning Outcome 4: Use nested lists.

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
Learning Outcome 5: Write the CBSE suggested programs — max/min/mean, linear search, frequency of elements.

17.9 Worked Programs

17.9.1 Maximum, Minimum, Mean of a list

Without built-ins — the logic CBSE expects you to show:
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.166666666666664
Using 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:

Approach 1 — using 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
Approach 2 — manual counting with a dictionary (preview of Chapter 19):
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

PropertyStringList
Brackets" " / ' '[ ]
Ordered & indexableYesYes
MutableNoYes
Element typescharacters onlyany type, can be mixed
Length can change?No (new string each time)Yes (in place)
Can be a dict key?Yes — immutableNo — mutable
Common methods unique to itupper, split, stripappend, 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 · in membership · len(L) length · slicing L[a:b:s].
  • Mutability: L[i] = x, slice-assign, and del all modify the list in place. b = a creates an alias, not a copy — use a.copy() or a[:].
  • 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, returns None), 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 with count() on set(L) or with a dictionary.
  • Biggest gotcha: L = L.sort() sets L to None. Use L.sort() alone, or L = sorted(L).
🧠Practice Quiz — test yourself on this chapter