StayTalentReady

Lists and Data Structures

Outcome 6: Lists and Data Structures · Blueprint Pillar 3 · PGCC INT-1700 (interim) · Download .docx

Objectives

Key terms

list
An ordered, mutable sequence of items in square brackets; supports duplicate values and indexing.
index
An integer position within a list or string; 0 = first, -1 = last, -2 = second to last.
slice
A sub-sequence obtained with [start:stop:step] notation; start inclusive, stop exclusive.
append()
Adds a single element to the end of a list in place.
pop()
Removes and returns the element at a given index (default: last element).
sort()
Sorts the list in ascending order in place; sort(reverse=True) sorts descending.
dictionary
An unordered collection of key-value pairs in curly braces: {'key': value}. Access by key.
tuple
An ordered, immutable sequence in parentheses; values cannot be changed after creation.
mutable
An object whose value can be changed after creation: list, dict, set are mutable.
immutable
An object that cannot be changed: str, int, float, bool, tuple are immutable.

The concept

A list is Python's most versatile data structure for storing ordered collections. It is defined with square brackets: grades = [85, 92, 78, 91, 88]. Each element has an integer index starting at zero. grades[0] returns 85; grades[-1] returns 88 (last element). A slice grades[1:4] returns [92, 78, 91] — indices 1, 2, and 3 (stop is exclusive).

Lists are mutable — you can change them after creation. grades[2] = 80 replaces the third element. grades.append(95) adds 95 at the end. grades.remove(92) removes the first occurrence of 92. grades.pop() removes and returns the last element; grades.pop(0) removes the first. grades.sort() rearranges in place in ascending order.

To iterate: for grade in grades: processes each element. enumerate gives both index and value: for i, grade in enumerate(grades): print(i, grade). List comprehension creates a new list: passing = [g for g in grades if g >= 70] collects all passing grades.

A dictionary maps unique keys to values: student = {'name': 'Jordan', 'gpa': 3.8, 'grade': 11}. Access by key: student['name'] returns 'Jordan'. Add: student['sport'] = 'track'. Delete: del student['sport']. Iterate keys: for key in student: print(key, student[key]). Common use: counting occurrences — create a dict, loop, increment counts.

A tuple is like a list but immutable: coordinates = (41.7, -73.9). Tuples cannot be changed after creation — you cannot append, remove, or reassign elements. Use tuples for data that should not change: GPS coordinates, RGB colors, database row records.

Blueprint Pillar 3 — Technology and Society: Nearly every real-world program manages collections of data — user accounts, product inventories, medical records, financial transactions. Lists and dictionaries are how Python programs store and process that data. A contact app is a list of dictionaries; a gradebook is a dictionary of lists. Knowing these structures and their methods is the bridge between toy programs and applications that solve real problems.

Worked examples

Example 1: Class average with list operations: scores = [88, 92, 75, 90, 83]. avg = sum(scores) / len(scores). high = max(scores). low = min(scores). print(f'Average: {avg:.1f}, High: {high}, Low: {low}'). Output: Average: 85.6, High: 92, Low: 75. Built-in sum(), max(), min() work directly on lists.
Example 2: Word frequency counter using a dictionary: text = 'to be or not to be'. words = text.split(). freq = {}. for word in words: if word in freq: freq[word] += 1 else: freq[word] = 1. print(freq). Output: {'to': 2, 'be': 2, 'or': 1, 'not': 1}. The dict stores counts keyed by word.

Common mistakes

Self-check

Try each one before you look. A miss here costs nothing and tells you exactly what to reread.

1. Given: nums = [10, 20, 30, 40]. What does nums[-1] return?
2. Which list method adds a single element to the end of the list?
3. How do you access the value for key 'name' in: person = {'name': 'Alex', 'age': 16}?
4. Which data structure in Python is IMMUTABLE?
5. What does len([5, 10, 15, 20]) return?

Canvas is the official record. This companion enhances the PGCC curriculum; it does not replace it. Last name and class year only. Students with a 504 plan or IEP: your accommodations apply.

← Functions and ModularityFile Input and Output →

↑ Back to top