Chapter 7: Detailed Study of Lists in Python
Comprehensive Curriculum Notes & Precautions
1 Introduction to Sequences
In Python, a sequence is an object containing multiple items of data stored one after another.
• Analogy: Months in a year, or cars in a sequence.
• Sequence Data Types: Python supports three primary types: Lists, Tuples, and Strings.
• Length: The total number of elements in a sequence.
2 The Python List - Characteristics
A list is a collection of comma-separated values (items) enclosed within square brackets [].
• Ordered: Elements have a defined position.
• Mutable: Values can be modified without creating a new list object.
• Heterogeneous: Items can be of any type (integers, strings, floats, nested lists).
• Duplicates: Lists allow repeated values.
3 Declaring and Initializing Lists
3.1 List Types
1. Empty List: L = [] (Has no elements).
2. Long List: Contains many elements (e.g., squares from 0 to 25).
3. Nested List: A list containing another list as an element: L = [3, 4, [5, 6, 7], 8].
3.2 Creation Methods
• Direct: L = [10, 20, 30]
• Constructor list():
– list() with no arguments creates an empty list.
– list('Computer') converts a string to characters: ['C', 'o', 'm', 'p', 'u', 't',
'e', 'r'].
• From User Input: L = list(input('Enter: ')) converts each character entered into a
list item.
1
4 Accessing Elements (Indexing)
4.1 Index Mechanics
• Forward Indexing: Starts from 0 to n − 1.
• Backward/Negative Indexing: Starts from -1 (last) to −n (first).
• Nested Indexing: For nested lists, use L[row][col]. Example: list1[4][1] accesses the
second element of the fifth element.
4.2 Precautions & Errors
• IndexError: Occurs if you try to access an index that does not exist (e.g., index 8 in a list
of 8 elements where max index is 7).
• Boundary Check: Slicing boundaries (start:stop) do not raise IndexError if out of bounds;
they simply return available elements.
5 Traversing a List
• Method 1 (for loop): for i in list1: print(i).
• Method 2 (range & len): for i in range(len(list1)): print(list1[i]).
• Method 3 (while loop): Requires manual initialization (index=0) and incrementing (index
+= 1).
6 Comparison and Operations
6.1 Operations
• Concatenation (+): Joins two lists. Precaution: You cannot concatenate a list with an
integer or string (e.g., List + 40 triggers a TypeError).
• Repetition (*): Replicates a list. Precaution: You cannot multiply a list by another list.
• Membership (in / not in): Used to check presence of an element.
6.2 Comparison Rules
• Lexicographical order: Element-by-element comparison.
• Type Matching: For equality (==), types must match. However, int 20 and float 20.0 are
considered equal.
7 List Methods and Built-in Functions
8 Important Precautions (CTM - Commit To Memory)
• Aliasing: L2 = L1 creates a reference, not a copy. Modifying one affects both.
• Cloning: Use L2 = L1[:] or [Link]() to create a separate object.
2
Method Precaution / Behavior
append(x) Adds one single element to the end. Modifies ”in-place”.
extend(L2) Merges elements of L2 into current list.
insert(i, x) Inserts x at index i. No exception for out-of-range index; in-
serts at nearest end.
pop(i) Removes and returns item at index i. Default is last item.
del L[i] Deletes element; does not return it.
remove(x) Removes the first occurrence of x. ValueError if x not found.
sort() Sorts ”in-place”. Strings are sorted by ASCII values.
sorted(L) Creates a new list; original remains unchanged.
max() / min() Elements must be of the same type to compare.
• Deletion: Using pop() or del with an out-of-range index results in a Runtime Error.
• Searching: index() returns only the first occurrence.