2. Arrays, Lists, and Dictionaries

2.1 Arrays

  • Fixed-size, homogeneous data structure.
  • Indexed using array[i], starting from 1 (in pseudocode) or 0 (in Python).
  • Use for vote counting, reverse printing, and parallel lists.
Two-Dimensional Arrays:
  • Access using array[row][col]
  • Used for tabular data (e.g., sales figures, game scores)

2.2 Lists (Python)

  • Mutable, ordered collections.
  • List operations: indexing, slicing, appending, inserting, deleting.
Methods: .append(), .extend(), .insert(), .remove(), .index(), .sort()
Example:

lst = [1, 2, 3]

lst.append(4) # [1, 2, 3, 4]

lst[1] = 10 # [1, 10, 3, 4]

Tuples:
  • Immutable version of lists: t = (1, 2, 3)

2.3 Dictionaries

  • Key-value pairs, unordered.
  • Keys must be immutable types (e.g., strings, numbers).
Methods: .get(), .pop(), .keys(), .values(), .items()
Example:

d = {“a”: 1, “b”: 2}

d[“c”] = 3

d.pop(“b”)