IS4010: AI-Enhanced Application Development

Week 4: Python Data Structures

Brandon M. Greenwell

Session 1

Sequences: lists & tuples

What is a data structure?

  • A data structure is a way to organize and store multiple pieces of data in a single variable.
  • Think of them as containers for your data.
  • Python gives us several powerful, built-in data structures, and choosing the right one for the job is a key programming skill.

The list: a mutable sequence

  • A list is an ordered, changeable collection of items. It is one of the most common data structures in python.
  • Ordered: the items maintain the position in which they were added.
  • Mutable: you can add, remove, or change items after the list has been created.
  • Syntax: items are enclosed in square brackets [].
# A list of student names
students = ["grace", "ada", "katherine"]

# We can add a new student
students.append("dorothy")

# We can change an item
students[0] = "grace hopper"

print(students)

The tuple: an immutable sequence

  • A tuple is an ordered, unchangeable collection of items.
  • Ordered: just like a list, items maintain their position.
  • Immutable: you can not add, remove, or change items after the tuple has been created.
  • Syntax: items are enclosed in parentheses ().
  • Use a tuple for data that should not change, like coordinates (x, y) or rgb color values (255, 0, 0).

Accessing items: indexing & slicing

  • You can access individual items in lists and tuples using their index. Python uses zero-based indexing.
  • my_list[0] gets the first item.
  • my_list[-1] gets the last item.
  • You can get a range of items, called a slice, by specifying a start and end index.
  • my_list[1:4] gets the items from index 1 up to (but not including) index 4.

Session 2

Mappings & collections: dictionaries & sets

The dictionary: key-value pairs

  • A dictionary is an unordered collection of key: value pairs.
  • They are optimized for retrieving a value when you know the key, similar to a real-world dictionary or a phone book.
  • Keys must be unique and are typically strings or numbers.
  • Syntax: pairs are enclosed in curly braces {}.
# A dictionary representing a user
user = {
    "first_name": "ada",
    "last_name": "lovelace",
    "birth_year": 1815
}

# Accessing a value by its key
print(user["first_name"])

The set: unique items

  • A set is an unordered collection of unique items.
  • Its primary purpose is to ensure that you don’t have any duplicate entries.
  • It is also very fast for checking if an item is present in the collection.
  • Syntax: items are enclosed in curly braces {}, but they are not key-value pairs.
# A list with duplicate numbers
numbers = [1, 2, 2, 3, 4, 4, 4, 5]

# Create a set from the list to get only unique items
unique_numbers = set(numbers)

# The result will be {1, 2, 3, 4, 5}
print(unique_numbers)

Choosing the right data structure

  • Use a list when you need an ordered collection of items that you might need to change later.
  • Use a tuple when you have an ordered collection of items that should never change.
  • Use a dict when you need to store and retrieve data by a unique key.
  • Use a set when you need to store a collection of unique items and don’t care about their order.

Introducing lab 04

  • This week’s lab will have you practice creating and manipulating all four of these data structures.
  • You will write functions that process lists, look up items in dictionaries, and remove duplicates using sets.
  • The automated tests in your ci/cd pipeline will verify that your code is working correctly.
  • Please navigate to labs/lab04/README.md for full instructions.