Unit 11.3A · Term 3

Data Structures Comparison & Revision

In Term 2 and Term 3, we studied four core compound data structures: Lists, Tuples, Sets, and Dictionaries. Selecting the optimal structure is the hallmark of great software design.

Learning Objectives

  • 11.2.3.6 Define the difference between different data structures
  • 11.2.3.3 Apply functions and methods of processing lists
  • 11.3.3.4 Solve applied problems from various subject areas

Lesson Presentation

11.3A-lesson-20-data-structures-revision.pdf · Slides for classroom use

Conceptual Anchor

The Power Tools Analogy

If basic operations are hand tools, methods are power tools. split() is a saw (cutting a log into planks), join() is glue, and sort() organizes your workshop instantly.

Rules & Theory

String Methods (11.2.2.3)

# Changing Case s = "Hello World" print(s.upper()) # "HELLO WORLD" print(s.lower()) # "hello world" print(s.title()) # "Hello World" print(s.swapcase()) # "hELLO wORLD" # Searching & Replacing print(s.find("World")) # 6 (index where it starts) print(s.replace("World", "Python")) # "Hello Python" print(s.count("l")) # 3 # Checking Content print("123".isdigit()) # True print("abc".isalpha()) # True print(" ".isspace()) # True # Splitting & Joining words = s.split() # ['Hello', 'World'] print("-".join(words)) # "Hello-World" # Stripping Whitespace raw = " user input " print(raw.strip()) # "user input"

List Methods (11.2.3.3)

lst = [3, 1, 4, 1, 5] # Adding Elements lst.append(9) # [3, 1, 4, 1, 5, 9] (add to end) lst.insert(0, 2) # [2, 3, 1, 4, 1, 5, 9] (insert at index 0) lst.extend([6, 7]) # [2, 3, 1, 4, 1, 5, 9, 6, 7] (add another list) # Removing Elements lst.pop() # Removes last element (7) lst.pop(0) # Removes element at index 0 (2) lst.remove(4) # Removes first occurrence of value 4 # lst.clear() # Removes all elements # Ordering lst.sort() # Sorts in-place: [1, 1, 3, 5, 6, 9] print(lst) lst.reverse() # Reverses in-place: [9, 6, 5, 3, 1, 1] # Information print(lst.count(1)) # 2 print(lst.index(9)) # 0 (index of first 9)

In-place vs Return

String methods return a new string (strings are immutable). List methods like sort() change the list in-place and return None.

Worked Examples

1 Palindrome Checker

def is_palindrome(text): # Remove spaces and convert to lower case clean_text = text.replace(" ", "").lower() # Compare with reverse return clean_text == clean_text[::-1] print(is_palindrome("Race car")) # True print(is_palindrome("Hello")) # False

2 Processing a List of Names

names = [" ali ", "DANA", "marat "] cleaned_names = [] for name in names: cleaned_names.append(name.strip().title()) cleaned_names.sort() print(cleaned_names) # ['Ali', 'Dana', 'Marat']

Pitfalls & Common Errors

Sorting Mistake

x = lst.sort() sets x to None! Determine if you want to modify the list (lst.sort()) or create a new sorted list (x = sorted(lst)).

String Immutability

s.upper() does NOT change s. You must assign it: s = s.upper().

Master Comparison: Python Data Structures

Data Structure Syntax Ordered? Mutable? Duplicates? Lookup Time
List [1, 2, 3] ✓ Yes ✓ Yes ✓ Allowed $O(n)$ by value, $O(1)$ by index
Tuple (1, 2, 3) ✓ Yes ✗ Immutable ✓ Allowed $O(n)$ by value, $O(1)$ by index
Set {1, 2, 3} ✗ No ✓ Yes ✗ Unique only $O(1)$ average (hash table)
Dictionary {"k": "v"} ✓ Yes (Python 3.7+) ✓ Yes Keys unique, values duplicated $O(1)$ by key

Pro-Tips for Exams

Exam Selection Matrix

  • Need ordered data that changes frequently? → Use List.
  • Need constant coordinate pairs or dictionary keys? → Use Tuple.
  • Need to eliminate duplicates or test membership instantly? → Use Set.
  • Need phonebook-style key-value association? → Use Dictionary.

Graded Tasks

Remember

Which Python collections are mutable, and which are immutable?

Understand

Explain why a list cannot be used as a key in a Python dictionary, while a tuple can.

Apply

Given a list with duplicate elements, write one line of code to produce a sorted list of unique elements.

Analyze

Compare the memory and speed performance of looking up an item in a list of 100,000 items versus a set of 100,000 items.

Create

Design a student record system that uses a dictionary where each key is a Student ID and each value is another dictionary storing lists of quiz grades.

Self-Check Quiz

1. Can a set contain duplicate elements?
Click to reveal answer
2. What happens if you try to modify an element in a tuple: t[0] = 99?
Click to reveal answer
3. What is the average time complexity of key in dict?
Click to reveal answer