Lists and Data Structures
Outcome 6: Lists and Data Structures · Blueprint Pillar 3 · PGCC INT-1700 (interim) · Download .docx
Objectives
- Create, access, and modify Python lists using index notation and methods.
- Iterate over a list using for loops and enumerate().
- Use list methods: append(), remove(), pop(), sort(), and len().
- Create and access dictionaries using key-value pairs.
- Distinguish mutable (list, dict) from immutable (tuple, str) data structures.
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
Common mistakes
- IndexError from accessing an index beyond the list length. grades[10] when len(grades) is 5 raises IndexError. Use len() and bounds-check before accessing by index.
- Confusing list.sort() (modifies in place, returns None) with sorted(list) (returns a new sorted list, original unchanged). result = grades.sort() assigns None to result.
- Using a list key in a dictionary access. Dictionaries use keys (any immutable type) — not integer positions. student[0] fails on a dictionary that has string keys; use student['name'].
Self-check
Try each one before you look. A miss here costs nothing and tells you exactly what to reread.
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.