Area 2.3

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.

3
Structures
15
Operations
~2hrs
Study Time

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 length

Common Operations:

Add item
list.append(item)fruits.append("mango")
Insert item
list.insert(index, item)fruits.insert(0, "berry")
Remove item
list.remove(item)fruits.remove("apple")
Access item
list[index]first = fruits[0]
Find length
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 length

Common Operations:

Create array
array.array(type, values)arr = array.array("i", [1,2,3])
Add element
array.append(value)arr.append(4)
Access element
array[index]value = arr[0]
Modify element
array[index] = valuearr[1] = 10
Get length
len(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 keys

Common Operations:

Add/Update
dict[key] = valuestudent["age"] = 21
Access value
dict[key]name = student["name"]
Remove item
del dict[key]del student["grade"]
Get keys
dict.keys()keys = student.keys()
Check key exists
key in dictif "name" in student:

Data Structure Comparison

FeatureListArrayDictionary
Access MethodBy index (0, 1, 2...)By index (0, 1, 2...)By key ("name", "age"...)
OrderMaintains orderMaintains orderInsertion order (Python 3.7+)
Data TypesMixed types allowedSingle type onlyKeys: hashable, Values: any
SizeDynamic (can grow/shrink)Fixed sizeDynamic (can grow/shrink)
PerformanceGood for most usesFastest for numeric dataFastest key-based lookup
Memory UsageModerateMost efficientHigher overhead

Learning Activities

Data Structure Selection Challenge

Decision
30 minutes

Choose the best data structure for various real-world scenarios

Collection Operations Practice

Practice
45 minutes

Implement adding, removing, and searching operations on different structures

Performance Comparison Experiment

Experiment
35 minutes

Compare access times and memory usage of lists, arrays, and dictionaries