Course 5: Four Data - Data Structures (Lists)
Course overview
In this course, students learn the an efficient way to manage multiple pieces of data. Lists are essential in games when you need to handle multiple items like inventories, enemy lists, and score records. Once you understand the concept of data structures, you can build more complex and larger-scale programs.
Estimated time: 8-16 hours
The time required depends on the student's grade level, prior experience, and learning speed, and varies greatly. It also differs between just learning the basic concepts and moving on, versus additional practice problems or creative projects: taking those on makes a difference too. The times above are an average range, so please proceed flexibly at your child's pace.
What abilities will it build?
the concept of "data structures" for storing and managing data . Every app, website, and AI in the world is built on structures that efficiently store and search data. A shopping site's product list, YouTube's video list, KakaoTalk's message history - they all use List structureIt is.
What principles will you learn?
- (sequential data) A list stores data in order. Using an index (0, 1, 2...) to directly access the data you wantis the basic principle of database lookups.
- (dynamic data) By adding with append() and removing with remove(), you handle data that changes in real time. A user adding and removing products from a cart, or enemies appearing and disappearing in a game - they all follow this principle.
AI-era thinking: computing (problem-solving) | data-driven | probabilistic thinking
Computational Thinking
| Skill | Description | Example activity |
|---|---|---|
| 📋 Data organization | Arrange multiple values in order | Manage an inventory item list |
| 👆 Indexing | Access a specific value by position number | items[0] → first item |
| ↕️ Dynamic expansion | Add/remove data while running | Remove an enemy from the list when defeated |
| 🔄 Iteration processing | Apply the same operation to every item | Update the position of every enemy |
Mathematical connections
| Math concept | Programming application | Learning benefit |
|---|---|---|
| Ordered pairs | scores = [100, 95, 87] |
Representing terms of a sequence |
| 0-indexing | list[0] = first |
Start counting from 0 (computer convention) |
| Length and range | len(list) = 5 → indices 0~4 |
n items → indices 0 ~ n-1 |
| Sum/average | sum(scores) / len(scores) |
Statistics basics |
Index calculation
| Expression | Meaning | Example (length 5) |
|---|---|---|
list[0] |
The first | 0th item |
list[-1] |
Last | 4th item |
list[len(list)-1] |
Last (calculated) | 5-1 = 4th |
list[2:4] |
Slicing | items 2 and 3 |
List operations
# Lists and math
scores = [90, 85, 92, 78, 88]
Total = sum(scores) # 433
Count = len(scores) # 5
average = sum(scores)/len(scores) # 86.6
Max = max(scores) # 92
Min = min(scores) # 78
Range = max(scores) - min(scores) # 14
Real-life connection
| Real life | List representation | Operation |
|---|---|---|
| Shopping list | ["milk", "bread", "eggs"] |
Add/remove |
| Test scores | [85, 90, 78, 92] |
Computing an average |
| Game ranking | ["1st", "2nd", "3rd"] |
Access by index |
💻 Code examples & visualization
Example 1: Inventory system
# Start with an empty inventory
inventory = []
# Get an item (append)
inventory.append("sword")
inventory.append("shield")
inventory.append("potion")
print(inventory) # ['sword', 'shield', 'potion']
# Access by index
print(inventory[0]) # 'sword' (first)
print(inventory[2]) # 'potion' (third)
# Use an item (remove)
inventory.remove("potion")
print(inventory) # ['sword', 'shield']
Inventory visualization:
append("sword") append("shield") append("potion")
↓ ↓ ↓
┌─────────┐ ┌─────────┐ ┌─────────┐
│ [0] swrd│ │ [0] swrd│ │ [0] swrd│
└─────────┘ │ [1] shld│ │ [1] shld│
└─────────┘ │ [2] potn│
└─────────┘
After remove("potion"):
┌─────────┐
│ [0] swrd│
│ [1] shld│
└─────────┘
Example 2: Managing an enemy list
from ursina import *
app = Ursina()
enemies = [] # Enemy list
# Create 5 enemies
for i in range(5):
enemy = Entity(
model='cube',
color=color.red,
position=(i * 2, 0, 5)
)
enemies.append(enemy) # Add to the list
# Move all enemies (iterate with a loop)
def update():
for enemy in enemies:
enemy.z -= 2 * time.dt # Every enemy moves forward
app.run()
Enemy list structure:
enemies = [enemy0, enemy1, enemy2, enemy3, enemy4]
│ │ │ │ │
▼ ▼ ▼ ▼ ▼
┌──┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐
│■ │ │■ │ │■ │ │■ │ │■ │
└──┘ └──┘ └──┘ └──┘ └──┘
x=0 x=2 x=4 x=6 x=8
for enemy in enemies: handles them all at once!
Example 3: Score system
scores = [100, 85, 92, 78, 95]
# Using list functions
print(f"Total score: {sum(scores)}") # 450
print(f"Average: {sum(scores)/len(scores)}") # 90.0
print(f"Highest score: {max(scores)}") # 100
print(f"Lowest score: {min(scores)}") # 78
print(f"Games: {len(scores)}") # 5
# Add a new score
scores.append(88)
print(f"New average: {sum(scores)/len(scores)}") # 89.67
Visualized as a bar graph:
100│ ██
95│ ██ ██
92│ ██ ██ ██
88│ ██ ██ ██ ██
85│ ██ ██ ██ ██ ██
78│ ██ ██ ██ ██ ██ ██
└─────────────────────────────
1 2 3 4 5 6
(added)
Example 4: Slicing
colors = ["Red", "orange", "yellow", "Green", "Blue", "indigo", "violet"]
# Slicing (extracting a part)
print(colors[0:3]) # ['red', 'orange', 'yellow']
print(colors[2:5]) # ['yellow', 'green', 'blue']
print(colors[-3:]) # ['blue', 'indigo', 'violet'] (last 3)
print(colors[::2]) # ['red', 'yellow', 'blue', 'violet'] (every 2nd)
Slicing visualization:
Index: 0 1 2 3 4 5 6
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│red │orng │yllw │grn │blue │indg │viol │
└─────┴─────┴─────┴─────┴─────┴─────┴─────┘
colors[0:3]:
├─────┬─────┬─────┤
│red │orng │yllw │ ← 0, 1, 2 (3 excluded)
└─────┴─────┴─────┘
colors[::2]:
├─────┼ ├─────┼ ├─────┼ ├─────┤
│red │ │yllw │ │blue │ │viol │
└─────┘ └─────┘ └─────┘ └─────┘
0 2 4 6
Example 5: Managing a bullet list
from ursina import *
app = Ursina()
player = Entity(model='cube', color=color.blue, y=-4)
bullets = [] # Bullet list
def input(key):
if key == 'space': # Fire with the spacebar
bullet = Entity(
model='sphere',
color=color.yellow,
scale=0.3,
position=player.position
)
bullets.append(bullet)
def update():
# Move all bullets
for bullet in bullets[:]: # Iterate over a copy
bullet.y += 10 * time.dt
# Delete when it goes off screen
if bullet.y > 10:
bullets.remove(bullet)
destroy(bullet)
app.run()
Bullet management flow:
flowchart TD
A[Press spacebar] --> B[Create bullet<br/>bullets.append]
B --> C[Move bullet<br/>y += speed × dt]
C --> D{y > 10?}
D -->|No| C
D -->|Yes| E[Delete bullet<br/>remove & destroy]
E -.-> C
Screen visualization:
Chapter 01: Lists
A. What Is a List?
| Item | Content |
|---|---|
| What will you learn? | Storing multiple pieces of data in one variable |
| Core Concepts | Collection, order, index |
Lists a row of boxes that store multiple values in orderIt is. inventory = ["검", "방패", "물약"], you hold multiple items in a single variable. Students learn that a variable can store multiple values, not just a single value. Each item in the list can be accessed by Index (number), and the first item is at 0. This follows the convention in computer science of starting to count from 0.
B. Making Lists
| Item | Content |
|---|---|
| What will you learn? | Creating and initializing lists |
| Core Concepts | Literals, empty lists, types |
scores = [100, 95, 87, 92]creates a number list, and names = ["Alice", "Bob"]creates a string list. You can also make an empty list items = []and add items later. Students learn that a list can hold data of the same type or different types. In games, it's used for many purposes, such as player lists, item inventories, and level score records various uses. You can check a list's length with len(scores).
C. append() - Adding items
| Item | Content |
|---|---|
| What will you learn? | Adding a new item to the end of a list |
| Core Concepts | Methods, dynamic expansion, mutability |
inventory.append("열쇠")adds a value to the adds a new item to the end. Students learn that a list's size is not fixed and changes dynamically. When you get an item in a game, you add it to your inventory with inventory.append(new_item). .append()is Method - an action that an object (a list) performs. This concept is covered more deeply later in object-oriented programming.
D. remove() - Removing items
| Item | Content |
|---|---|
| What will you learn? | Deleting a specific item from a list |
| Core Concepts | Deletion, searching, exception handling |
inventory.remove("물약")is removes the first item with that value. Students learn that data can be deleted as well as added. When you use an item or defeat an enemy in a game, you remove it from the list. They also learn that trying to remove an item that doesn't exist causes an error. For safe code, they build the habit of checking whether an item exists before deleting it.
E. Loops and Lists
| Item | Content |
|---|---|
| What will you learn? | Processing every item in a list |
| Core Concepts | Iteration, batch processing |
for item in inventory:with processes every item in the list one by one. This means "for each item in the inventory, do ~." Students learn that lists and loops are a perfect pair. You use this pattern when updating the position of every enemy, summing all the scores, or displaying all items on screen. This is the most basic and powerful pattern in data processingIt is.
Why are lists important?
Real-life connection
Lists are the most commonly used data structure in programming:
| Real-life example | Programming example |
|---|---|
| Shopping list | Shopping cart cart = ["우유", "빵"] |
| Class roster | User list users = ["Alice", "Bob"] |
| Music playlist | Playlist playlist = [song1, song2] |
| To-do list | To-do list todos = ["숙제", "청소"] |
When you finish this course...
Learning outcomes
Students will be able to:
- Multiple pieces of data into one list: manage with
- In the list, Add and remove items
- Index: access a specific item with
- Process every item in a list all items
- A game's inventory, enemy list, and score records : implement things like
- ready to learn more complex data structures (dictionaries, 2D arrays, etc.) more complex game logic (movement, collisions, etc.)