Data Structures
Master the fundamental data structures: lists, arrays, and dictionaries. Learn when to use each structure and how to perform basic operations on collections of data.
Learning Objectives
- Understand lists, arrays, and dictionary data structures
- Know when to use each data structure based on data organization needs
- Perform basic operations on collections of data
- Apply appropriate data structure selection for different scenarios
- Implement adding, removing, and accessing elements in data structures
Core Data Structures
Lists
Ordered collections of items that can be modified (mutable)
Characteristics:
- Ordered sequence
- Mutable (can change)
- Allow duplicates
- Indexed by position
When to Use:
Shopping lists, items, student grades, any ordered collection that may change
Python Example:
# Creating lists
fruits = ["apple", "banana", "orange"]
numbers = [1, 2, 3, 4, 5]
mixed = ["hello", 42, True, 3.14]
# Basic operations
fruits.append("grape") # Add to end
fruits.insert(1, "kiwi") # Insert at position
first_fruit = fruits[0] # Access by index
fruits.remove("banana") # Remove by value
length = len(fruits) # Get lengthCommon Operations:
list.append(item)fruits.append("mango")list.insert(index, item)fruits.insert(0, "berry")list.remove(item)fruits.remove("apple")list[index]first = fruits[0]len(list)count = len(fruits)Arrays
Fixed-size collections of items of the same data type
Characteristics:
- Fixed size
- Same data type
- Memory efficient
- Fast access by index
When to Use:
Mathematical calculations, image data, sensor readings, performance-critical applications
Python Example:
# Arrays in Python (using array module)
# Create typed arrays
integers = array.array('i', [1, 2, 3, 4, 5]) # 'i' = signed int
floats = array.array('f', [1.1, 2.2, 3.3]) # 'f' = float
# Basic operations
integers.append(6) # Add element
value = integers[0] # Access by index
integers[1] = 10 # Modify element
length = len(integers) # Get lengthCommon Operations:
array.array(type, values)arr = array.array("i", [1,2,3])array.append(value)arr.append(4)array[index]value = arr[0]array[index] = valuearr[1] = 10len(array)size = len(arr)Dictionaries
Collections of key-value pairs for mapping relationships
Characteristics:
- Key-value pairs
- Unordered (Python 3.7+ maintains insertion order)
- Keys must be unique
- Fast lookup by key
When to Use:
Student records, configuration settings, word definitions, any key-to-value mapping
Python Example:
# Creating dictionaries
student = {"name": "Alice", "age": 20, "grade": "A"}
prices = {"apple": 1.50, "banana": 0.80, "orange": 2.00}
# Basic operations
student["email"] = "alice@school.com" # Add new key-value
name = student["name"] # Access by key
student["age"] = 21 # Modify value
del student["grade"] # Remove key-value pair
keys = list(student.keys()) # Get all keysCommon Operations:
dict[key] = valuestudent["age"] = 21dict[key]name = student["name"]del dict[key]del student["grade"]dict.keys()keys = student.keys()key in dictif "name" in student:Data Structure Comparison
| Feature | List | Array | Dictionary |
|---|---|---|---|
| Access Method | By index (0, 1, 2...) | By index (0, 1, 2...) | By key ("name", "age"...) |
| Order | Maintains order | Maintains order | Insertion order (Python 3.7+) |
| Data Types | Mixed types allowed | Single type only | Keys: hashable, Values: any |
| Size | Dynamic (can grow/shrink) | Fixed size | Dynamic (can grow/shrink) |
| Performance | Good for most uses | Fastest for numeric data | Fastest key-based lookup |
| Memory Usage | Moderate | Most efficient | Higher overhead |
Learning Activities
Data Structure Selection Challenge
Choose the best data structure for various real-world scenarios
Collection Operations Practice
Implement adding, removing, and searching operations on different structures
Performance Comparison Experiment
Compare access times and memory usage of lists, arrays, and dictionaries