Class XI · Chapter 9Unit 2, Python Programming (45 marks)6 min read
Chapter 9: Lists
CBSE Unit: Unit 2, Python Programming (45 marks) Marks Weightage: ~6-8 marks Priority: CRITICAL, list operations, methods, and programs heavily tested
Key Concepts
9.1 Introduction to Lists
- Ordered, mutable sequence of elements, Can contain mixed data types (int, float, string, list), Elements enclosed in square brackets [], separated by commas, Indices start from 0 (positive) or -1 (negative, from end)
list1 = [2, 4, 6, 8, 10] # integers
list2 = ['a', 'e', 'i', 'o', 'u'] # strings
list3 = [100, 23.5, 'Hello'] # mixed types
list4 = [['Phys', 101], ['Chem', 202]] # nested list
list5 = [] # empty list
9.2 Accessing Elements
L = [10, 20, 30, 40, 50]
L[0] # 10 (first element)
L[-1] # 50 (last element)
L[2] # 30
9.3 List Slicing
L = [10, 20, 30, 40, 50]
L[1:4] # [20, 30, 40]
L[:3] # [10, 20, 30]
L[2:] # [30, 40, 50]
L[:] # [10, 20, 30, 40, 50] (copy)
L[::2] # [10, 30, 50]
L[::-1] # [50, 40, 30, 20, 10] (reverse)
9.4 List Operations
| Operation | Syntax | Example | Result |
|---|---|---|---|
| Concatenation | + |
[1,2] + [3,4] |
[1,2,3,4] |
| Repetition | * |
[0] * 3 |
[0,0,0] |
| Membership | in |
3 in [1,2,3] |
True |
| Length | len() |
len([1,2,3]) |
3 |
| Max | max() |
max([1,2,3]) |
3 |
| Min | min() |
min([1,2,3]) |
1 |
| Sum | sum() |
sum([1,2,3]) |
6 |
| Sorted | sorted() |
sorted([3,1,2]) |
[1,2,3] |
9.5 List is Mutable
L = [10, 20, 30]
L[1] = 25 # L = [10, 25, 30] (individual element changed)
L[0:2] = [5, 15] # L = [5, 15, 30] (slice replaced)
9.6 Traversing a List
L = [10, 20, 30]
# Method 1: Direct
for item in L:
print(item)
# Method 2: Using index
for i in range(len(L)):
print(L[i])
9.7 List Methods
| Method | Description | Example |
|---|---|---|
append(x) |
Add element at end | L.append(40) |
extend(list) |
Add all elements of list at end | L.extend([50,60]) |
insert(i, x) |
Insert x at index i | L.insert(1, 15) |
remove(x) |
Remove first occurrence of x | L.remove(20) |
pop() |
Remove and return last element | L.pop() |
pop(i) |
Remove and return element at index i | L.pop(0) |
clear() |
Remove all elements | L.clear() |
index(x) |
Return index of first occurrence of x | L.index(30) |
count(x) |
Count occurrences of x | L.count(10) |
sort() |
Sort in ascending order (in-place) | L.sort() |
sort(reverse=True) |
Sort in descending order | L.sort(reverse=True) |
reverse() |
Reverse the list (in-place) | L.reverse() |
copy() |
Return shallow copy | L2 = L.copy() |
9.8 List vs String
| Feature | List | String |
|---|---|---|
| Mutability | Mutable | Immutable |
| Elements | Any data type | Characters only |
| Syntax | [1, 2, 3] |
"abc" |
| Modification | L[0] = 5 works |
s[0] = 'h' error |
9.9 Nested Lists
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
matrix[0] # [1, 2, 3]
matrix[0][1] # 2
matrix[1][2] # 6
9.10 Copying Lists
L1 = [1, 2, 3]
L2 = L1 # L2 refers to SAME list (alias, not copy)
L3 = L1.copy() # L3 is a NEW copy
L4 = L1[:] # L4 is also a NEW copy
L5 = list(L1) # L5 is also a NEW copy
Important: L2 = L1 creates an alias, changes to L2 affect L1!
9.11 List as Function Argument, Lists are passed by reference (actually by object reference), Changes to list inside function affect the original list
def change(L):
L.append(4)
mylist = [1, 2, 3]
change(mylist)
print(mylist) # [1, 2, 3, 4] -- original changed!
9.12 List Comprehension
squares = [x**2 for x in range(1, 6)] # [1, 4, 9, 16, 25]
evens = [x for x in range(10) if x % 2 == 0] # [0, 2, 4, 6, 8]
Programs/Code Examples
Find Largest Element
L = eval(input("Enter list: "))
print("Largest:", max(L))
# OR manually:
largest = L[0]
for item in L:
if item > largest:
largest = item
print("Largest:", largest)
Linear Search
L = [10, 20, 30, 40, 50]
key = int(input("Enter element to search: "))
found = False
for i in range(len(L)):
if L[i] == key:
print("Found at index", i)
found = True
break
if not found:
print("Not found")
Count Frequency of Elements
L = [1, 2, 3, 2, 1, 3, 3, 1]
for item in set(L):
print(item, ":", L.count(item))
Common Board Exam Question Patterns
- Find output of list operations/methods (2-4 marks)
- Write a program using lists (3-5 marks): search, sort, frequency
- Difference between append() and extend(), sort() and sorted(), remove() and pop() (2 marks)
- List aliasing vs copying (2-3 marks): What happens when L2 = L1
- Nested list access (1-2 marks)
- List as function argument (2-3 marks): Mutable object behavior
Key Points Students Miss
L2 = L1creates an alias (both point to same list), NOT a copysort()modifies in-place and returnsNone;sorted()returns a new listappend()adds one element (even if it's a list);extend()adds each elementpop()returns the removed element;remove()returnsNoneremove()removes by value;pop()removes by index- Lists are mutable, can change, add, delete elements after creation
del L[i]deletes element at index i;del L[1:3]deletes a slice- List passed to function: changes inside function affect original
L * 0returns empty list[]inoperator checks membership:3 in [1,2,3]is True
Prefer watching over reading?
Subscribe for free.