# Basic Data Collections

Computer Science I · Programming I · https://tryals.app/learn/computer-science-i/basic-data-collections

## Introduction to Collections

A single variable holds one value, but a **collection** holds many. The most common collection is the **list** (or array), an ordered sequence of values stored under one name.

`scores = [90, 85, 70, 100]`

Each element occupies a position called its **index**. Indices almost universally start at **0**: `scores[0]` evaluates to `90`, while the final element of a four-item list sits at `scores[3]` rather than `scores[4]`.

The **length** of a list is its total number of elements. Lists support adding, removing, updating items, and iterating through them using loops.

## Lists vs Dictionaries

A **dictionary** (or map) stores **key-value pairs**, allowing fast lookups by name or ID instead of numeric position.

`ages = {"Ana": 30, "Ben": 25}`

| Feature | **List** | **Dictionary** |
| --- | --- | --- |
| **Access** | By numeric index | By unique key |
| **Order** | Strictly ordered sequence | Unordered by position |
| **Use Case** | Ordered items & sequences | Key-based lookups |

**Common Pitfall:** Forgetting that indices start at **0**. For a list of length `n`, valid indices range from `0` to `n-1`. Accessing index `n` triggers an **index-out-of-range** error.

## Practice questions

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

### 1. Select every statement that is true of lists and dictionaries as the lesson describes them.

A. Sets do not allow duplicate elements.
B. Indexing starts at zero, so the final position of a four-element list is 3.
C. Lists preserve insertion order.
D. A dictionary maps keys to values.
E. Membership testing is faster in a set than in a list.

**Answer:** A. Sets do not allow duplicate elements.; B. Indexing starts at zero, so the final position of a four-element list is 3.; E. Membership testing is faster in a set than in a list.

**Why:** Lists are ordered and 0-indexed, so the last of n elements is at n minus 1; dictionaries are keyed by meaningful keys, not positions. Option 2 confuses a dictionary with a list, and option 4 makes the off-by-one error at the boundary.

Page: https://tryals.app/practice/computer-science-i/basic-data-collections/select-every-statement-that-is-true-of-lists-and-dictionaries-as-the

### 2. A list preserves order and allows duplicates; a set does neither and can test membership far faster. Why does giving up order and duplicates buy speed?

A. Because a set can store elements by hash, so membership is a direct lookup.
B. Because sets are stored in faster memory than lists.
C. Because sets are sorted, so membership can be tested by binary search.
D. Because sets contain fewer elements than lists, making them quicker to search.

**Answer:** A. Because a set can store elements by hash, so membership is a direct lookup.

**Why:** A list promises that element i is at position i, which fixes where everything is stored and leaves nothing to exploit. A set makes no such promise, and that freedom is what it trades for speed, hash the element and compute the location. A sorted structure is a third point on the same trade-off, giving O(log n) membership *and* ordered traversal.

Page: https://tryals.app/practice/computer-science-i/basic-data-collections/a-list-preserves-order-and-allows-duplicates-a-set-does-neither-and

### 3. For a list of length 4 (valid indices 0 through 3), sort each index by whether accessing it is valid.

**Answer:**

- Valid index: 0, 3, 2
- Out of range: 4, 5

**Why:** Valid indices run 0 to 3 for a four-item list, so 0, 2 and 3 are fine while 4 and 5 run off the end. Accessing index 4 here is the index-out-of-range error, and it stems from forgetting that counting starts at 0.

Page: https://tryals.app/practice/computer-science-i/basic-data-collections/for-a-list-of-length-4-valid-indices-0-through-3-sort-each-index

### 4. A list stores elements by sequential position, whereas a dictionary associates values with unique keys. What design consequence follows from this difference when choosing how to represent entity data?

A. A dictionary requires meaningful identifiers rather than sequence
B. A list prevents duplicate items from appearing in consecutive slots
C. A dictionary automatically orders entries by their key identifier
D. A list enables instant value retrieval without specifying position

**Answer:** A. A dictionary requires meaningful identifiers rather than sequence

**Why:** Positional sequences rely on an item's place in line, whereas map-like structures depend entirely on distinct lookup labels. Dictionaries do not sort their entries numerically or alphabetically, nor do lists restrict repeated elements or permit arbitrary retrieval without an index.

Page: https://tryals.app/practice/computer-science-i/basic-data-collections/a-list-stores-elements-by-sequential-position-whereas-a-dictionary
