2.3.9. Collection Types#
Python’s data structure are the built‑in container types you use to store, organize, and operate on groups of items/values. The four main data structure types are: list, tuple, dictionary, and set.
The commonly used built-in/standard data structures can be grouped as:
Sequence Types
Set Types
Mapping Types
Collections (broad category)
├── Sequence Types (ordered, indexed)
│ ├── list
│ ├── tuple
│ ├── str (string)
│ ├── range
│ └── bytes/bytearray
│
├── Set Types (unordered, no duplicates)
│ ├── set
│ └── frozenset
│
└── Mapping Types (key-value pairs)
└── dict
import sys
from pathlib import Path
# Find project root by looking for _config.yml
current = Path.cwd()
for parent in [current, *current.parents]:
if (parent / '_config.yml').exists():
project_root = parent
break
else:
project_root = Path.cwd().parent.parent
# Add project root to path
sys.path.insert(0, str(project_root))
# Import shared teaching helpers and cell magics
from shared import thinkpython, diagram, jupyturtle, structshape
from shared.download import download
Among them, list, tuple, and range are sequence types . Also, strings are considered a sequence type (a kind of collection) because they behave like collections of characters, although conceptually, in many languages other than Python, strings are considered primitives, not collections.
range is debatable as a “collection”; it’s really a lazy sequence generator, not a traditional data structure. It does look like a sequence, so Python treats it as one. Note that range stores a formula, not data; it computes values on demand. Therefore, range is a sequence protocol implementer, not a data container.
A set object is an “unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference” [cite:pPython Standard Library_2026].
Mapping in Python refers to data types that store key-value pairs, where each key is associated with a corresponding value. The most common mapping type is the dictionary (dict).
2.3.9.1. Mutability#
Mutability and order are two important characteristics of Python data structures, summarized in the table below.
Type |
Literal |
|
Ordered |
Usage |
|---|---|---|---|---|
Sequence Types |
||||
list |
|
Yes |
Yes |
General purpose; dynamic arrays; index/slice access. |
tuple |
|
No |
Yes |
Fixed records; function returns; hashable if elements are.* |
range |
|
No |
Yes |
Memory-efficient integer sequences; iteration. |
str |
|
No |
Yes |
Text; immutable character sequences. |
Set Types |
||||
set |
|
Yes |
No |
Unique items; membership testing; set operations. |
frozenset |
|
No |
No |
Immutable set; dict keys; set elements. |
Mapping Types |
||||
dict |
|
Yes |
Yes** |
Key-value lookups; counting; grouping; configuration. |
Notes:
* Tuples are hashable only if all elements are hashable.
** Dict ordering guaranteed in Python 3.7+.
2.3.9.2. Lists (Ordered Collections)#
Lists store multiple items in order and are mutable (can be changed):
numbers = [1, 2, 3, 4, 5]
fruits = ["Apple", "Banana", "Cherry"]
mixed = [1, "hello", 3.14, True]
empty_list = []
print(f"Numbers: {numbers}")
print(f"Fruits: {fruits}")
print(f"Mixed: {mixed}")
print(f"Empty list: {empty_list}")
Numbers: [1, 2, 3, 4, 5]
Fruits: ['Apple', 'Banana', 'Cherry']
Mixed: [1, 'hello', 3.14, True]
Empty list: []
Common list operations:
Append:
numbers.append(6)→[1, 2, 3, 4, 5, 6]Access/Indexing:
numbers[0]→1Slicing:
numbers[1:3]→[2, 3]Length:
len(numbers)→5Update:
numbers[0] = 99→[99, 2, 3, 4, 5, 6]
fruits = ["apple", "banana", "cherry"] ### create a list by assignment
print(f"\nOriginal list: {fruits}")
fruits.append("date") ### mutable: update/change the list
print(f"After append: {fruits}")
print(f"First fruit: {fruits[0]}") ### indexing
print(f"Number of fruits: {len(fruits)}") ### length
Original list: ['apple', 'banana', 'cherry']
After append: ['apple', 'banana', 'cherry', 'date']
First fruit: apple
Number of fruits: 4
fruits[2] = "cranberry" ### lists are mutable: modify an element
print(f"After modification: {fruits}")
After modification: ['apple', 'banana', 'cranberry', 'date']
2.3.9.3. Tuples (Immutable Sequences)#
Tuples are similar to lists, but they cannot be changed after creation (immutable). So, use tuples when you want to ensure data won’t change:
rgb_color = (255, 128, 0) single = (42,) # Note the comma for single-item tuple
fruits = tuple(fruits)
print(f"\nConverted to tuple: {fruits}") ### ( )
print("first element in coordinate: ", fruits[0]) ### indexing
Converted to tuple: ('apple', 'banana', 'cranberry', 'date')
first element in coordinate: apple
Since tuples are immutable, the following operation would generate an error:
%%expect TypeError
fruits[2] = "cherry" # This would cause an error because tuples are immutable!
TypeError: 'tuple' object does not support item assignment
2.3.9.4. Dictionaries (Key-Value Pairs)#
In Python, a mapping type is a collection that stores data as key–value pairs, where each key is unique and maps to a corresponding value. The most common mapping type is the dictionary (dict), which allows flexible data organization and fast lookup, insertion, and modification of values by key rather than numerical index. An example of a Python dictionary:
student = {
"name": "Alice",
"age": 20,
"major": "IST"
}
student
{'name': 'Alice', 'age': 20, 'major': 'IST'}
Common dictionary operations:
Access:
student["name"]→"Alice"Add/Update:
student["gpa"] = 3.8Keys:
student.keys()→dict_keys(['name', 'age', 'major'])Values:
student.values()→dict_values(['Alice', 20, 'Computer Science'])
print(f"Student Name: {student['name']}") ### 1. access value by key
student["major"] = "CS" ### 2. update: MUTABLE
student["GPA"] = 3.5 ### 2. add new key-value pair: MUTABLE
print(f"student: {student}")
print(student.keys()) ### 3. keys
print(student.values()) ### 4. values
Student Name: Alice
student: {'name': 'Alice', 'age': 20, 'major': 'CS', 'GPA': 3.5}
dict_keys(['name', 'age', 'major', 'GPA'])
dict_values(['Alice', 20, 'CS', 3.5])
2.3.9.5. Sets#
A set is an unordered collection of unique elements. Sets are useful for removing duplicates and performing mathematical set operations.
Key characteristics:
Unordered: Elements have no defined order
Unique: Automatically removes duplicate values
Mutable: Can add/remove elements
No indexing: Cannot access elements by position
# Creating sets
unique_numbers = {1, 2, 3, 4, 5}
print(f"Set: {unique_numbers}")
# Duplicates are automatically removed
data = {5, 2, 8, 2, 5, 9}
print(f"Unique values: {data}")
# Creating from a list
measurements = [23.5, 24.1, 23.5, 25.0]
unique = set(measurements)
print(f"From list: {unique}")
Set: {1, 2, 3, 4, 5}
Unique values: {8, 9, 2, 5}
From list: {24.1, 25.0, 23.5}
# Basic set operations
set_a = {1, 2, 3}
set_b = {3, 4, 5}
print(f"Union: {set_a | set_b}") # All elements
print(f"Intersection: {set_a & set_b}") # Common elements
print(f"Difference: {set_a - set_b}") # In A but not B
Union: {1, 2, 3, 4, 5}
Intersection: {3}
Difference: {1, 2}