# Lists, arrays, and structured storage

Physics I · Computer Science · https://tryals.app/learn/physics-i/lists-arrays-and-structured-storage

## Python Lists & NumPy Arrays

Physics simulations handle collections like particle positions or time series. Python provides **lists** while NumPy provides **arrays**.

**Python lists** are ordered, mutable, and can hold mixed types. They use 0-indexing: `temperatures[0]` gets the first item, and `len()` gives the count.

**NumPy arrays** are homogeneous, fast, and support vectorised operations. Functions like `np.linspace` generate points, and operations apply element-wise.

| Feature | Python Lists | NumPy Arrays |
| --- | --- | --- |
| **Types** | Mixed / Heterogeneous | Homogeneous |
| **Speed** | Interpreted Python | Compiled C |
| **Memory** | Object overhead | Raw compact data |

> **Tip:** Use `np.array` for numerical computation and Python `list` for heterogeneous or small collections.

## Slicing, Speed & Pitfalls

**Slicing** extracts subsets using **start:stop:step** syntax. For example, `data[1:4]` returns indices 1, 2, and 3, while `data[::2]` takes every second element.

**Broadcasting** allows math operations like `a * 2` to multiply every element without writing an explicit loop.

| Operation | Syntax Example | Result |
| --- | --- | --- |
| **Basic Slice** | `data[1:4]` | `[20, 30, 40]` |
| **Strided Slice** | `data[::2]` | `[10, 30, 50]` |

> **Common pitfall:** Assigning a list copies a **reference**, not the data. After `b = a`, mutating `b` mutates `a` too. Always use explicit copies like `list(a)` or slicing to fork data.

## Practice questions

5 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. Accessing an element by index in an array takes O(n) time.

**Answer:** False

**Why:** Direct index access is O(1) because the address is computed from the base pointer.

Page: https://tryals.app/practice/physics-i/lists-arrays-and-structured-storage/accessing-an-element-by-index-in-an-array-takes-o-n-time

### 2. Appending to the end of a list is cheap; inserting at the front is expensive. What about the storage layout causes the asymmetry?

A. Contiguous storage: an insertion at the front shifts everything
B. Lists are stored in reverse, so the front is the hardest position to reach
C. Insertion at the front requires re-sorting the list
D. The front of a list is protected and requires a permission check

**Answer:** A. Contiguous storage: an insertion at the front shifts everything

**Why:** Cost follows layout. This is exactly why a deque or linked list exists, different layouts make different operations cheap, and the choice of structure is a choice about which operations you will do most.

Page: https://tryals.app/practice/physics-i/lists-arrays-and-structured-storage/appending-to-the-end-of-a-list-is-cheap-inserting-at-the-front-is

### 3. After `b = a` and `b.append(4)`, printing `a` shows the **4 as well**. What has actually happened?

A. Both names refer to one list; assignment did not copy
B. Appending to a list always modifies every list in the program
C. `b` was created as a copy, but appending writes through to the original
D. The lists were merged automatically because they held the same values

**Answer:** A. Both names refer to one list; assignment did not copy

**Why:** Assignment binds a name, it does not duplicate an object, so `a` and `b` are two labels on one list, and mutating through either is visible through both. This is why `.copy()` and `list(a)` exist, and why a function that mutates a list argument changes its caller’s data with no return value in sight.

Page: https://tryals.app/practice/physics-i/lists-arrays-and-structured-storage/after-b-a-and-b-append-4-printing-a-shows-the-4-as-well-what-has

### 4. Which operations modify a Python list in place?

A. len(lst)
B. sorted(lst)
C. reverse()
D. append(x)

**Answer:** C. reverse(); D. append(x)

**Why:** sorted() returns a new list; len() only reads. append and reverse mutate.

Page: https://tryals.app/practice/physics-i/lists-arrays-and-structured-storage/which-operations-modify-a-python-list-in-place

### 5. Sort these Python types into Mutable or Immutable.

**Answer:**

- Mutable: list, dict, set
- Immutable: tuple, str

**Why:** Tuples and strings cannot be changed after creation; lists, dicts, and sets can.

Page: https://tryals.app/practice/physics-i/lists-arrays-and-structured-storage/sort-these-python-types-into-mutable-or-immutable
