Lists#
Ordered, mutable collection: L = [10, 20, 30]. Indexing and slicing work like strings. Elements can be changed: L[0] = 5.
| Method/function | Does |
|---|---|
| append(x) | add at end |
| extend(list) | add many |
| insert(i, x) | add at position |
| remove(x) | remove first x |
| pop(i) | remove and return (last by default) |
| sort(), reverse() | in place |
| count(x), index(x) | search |
| len, max, min, sum, sorted | built-in functions |
सूची (list) परिवर्तनशील होती है - इसके तत्व बदले, जोड़े या हटाए जा सकते हैं। append() अंत में जोड़ता है, insert() किसी स्थान पर, remove() मान से हटाता है और pop() स्थान से हटाकर लौटाता है।
Nested lists and copying#
M = [[1, 2], [3, 4]]; M[1][0] is 3. Assignment B = A makes both names refer to the same list; use A.copy() or list(A) for a separate copy.
Exam tips#
- Programs: linear search, frequency of elements, separate even/odd, find second largest.