Class 11 · Computer Science · Computer Science
Chapter 9: Lists Notes
The big idea
A list is an ordered, mutable sequence that can hold mixed data types; it supports indexing, slicing, many methods, nesting, copying and passing to functions by reference.
Things to remember
- A list is an ordered, mutable sequence written in square brackets with commas, such as [100, 23.5, 'Hello']. It can hold mixed types, including another list (nested list). Indices start at 0, and list1[-1] is the last element.
- Lists are mutable, so list1[3] = 'Black' changes an element in place. An index outside the range gives IndexError. Nested access uses two indices: for [1, 2, 'a', 'c', [6, 7, 8], 4, 9], the value of list1[4][1] is 7.
- Operations: + joins two lists only (list plus string gives TypeError), * repeats ([6, 7, 8, 9] * 2 gives the elements twice), and in or not in test membership. Slicing like list1[2:6] and list1[::-1] works exactly as for strings.
- Adding: append(x) adds one element, even a list, at the end; extend(seq) adds each element of another list; insert(i, x) places x at index i. Example: [10, 20, 30] extended by [40, 50] gives [10, 20, 30, 40, 50].
- Removing and searching: remove(x) deletes the first x (ValueError if absent); pop(i) removes and returns the element at index i, or the last if none is given; count(x), index(x), reverse(), sort() in place, sorted() returns a new list; min, max and sum also work.
- Traverse with for item in list1, or for i in range(len(list1)), or a while loop on an index. The statement list2 = list1 creates an alias, not a copy; changes through one name show in the other.
- To get a real copy use list1[:], list(list1) or copy.copy(list1). After a = [1, 2, 3], b = a and a.append(10), b also becomes [1, 2, 3, 10], whereas c = a[:] stays independent of later changes.
- A list passed to a function is passed by reference: changes to its elements inside the function show in the caller. But assigning a brand new list to the parameter creates a local list and leaves the original unchanged.
Words to know
Tap a card to see what it means
Quick quiz
⭐ 0 / 5list1 = [10, 20, 30]. After list1.append([40, 50]), what is len(list1)?
After myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and del myList[::2], what does myList contain?
a = [1, 2, 3]; b = a; a.append(10); c = a[:]; a.append(99). What are b and c now?
Assertion (A): After increment(list1), where the function adds 5 to each element using list2[i] += 5, the original list1 shows the changed values. Reason (R): A list passed to a function is passed as a reference to the same list object. Which is correct?
For list1 = [10, 20, 30, 40, 50, 60], x = list1.pop(3) gives:
Match the pairs
Match each list method with what it does.
Try this at home
Write your family's five favourite foods as a list on paper, then act out append, insert, remove and reverse on it. Count the index of each item from 0 and from -1 to see both numbering ways.
All Computer Science notesChapter page and exercisesComputer Science syllabus