Computer Science Grade 8 20 min

Lesson 3: Lists: The Ordered Collection

Introduce lists as a basic data structure for storing an ordered sequence of items.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define what a list is and explain its characteristics as an ordered collection. Create lists and initialize them with various data types. Access individual elements and sub-sections of a list using indexing and slicing. Modify, add, and remove elements from a list using common list methods. Iterate through a list using a loop to process its elements. Explain the concept of mutability in the context of lists. Apply lists to solve simple programming problems, such as storing a collection of items. Ever wondered how your favorite music app keeps track of your playlist in order? 🎶 Or how a game remembers all the items in your inventory? 🤔 In this lesson, we'll dive into 'Lists,' a fundamental data structure in computer science. You'll...
2

Key Concepts & Vocabulary

TermDefinitionExample ListAn ordered, mutable collection of items. Items in a list can be of different data types (e.g., numbers, strings, booleans).my_shopping_list = ['milk', 'eggs', 'bread', 'cheese'] ElementAn individual item stored within a list.In `my_shopping_list`, 'milk' is an element, 'eggs' is another element. IndexA numerical position of an element within a list, starting from 0 for the first element. Used to access specific elements.In `my_shopping_list`, 'milk' is at index 0, 'eggs' is at index 1. `my_shopping_list[0]` would give 'milk'. MutabilityThe ability of a data structure to be changed after it has been created. Lists are mutable, meaning you can add, remove, or modify elements.A...
3

Core Syntax & Patterns

Creating and Initializing a List Lists are defined using square brackets `[]`, with elements separated by commas. To create an empty list, use `[]`. To create a list with initial values, place the values inside the brackets. Example: `my_list = [item1, item2, item3]` Accessing List Elements (Indexing) Use square brackets with an integer index to access an element: `list_name[index]`. Indexes start at 0 for the first element. Negative indexes can be used to count from the end, where -1 is the last element. Example: `my_list[0]` for the first element, `my_list[-1]` for the last. Modifying List Elements Assign a new value to an element at a specific index: `list_name[index] = new_value`. Because lists are mutable, you can change individual elements directly using their...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
Predict the final value of `numbers` after this loop runs. `numbers = [10, 20, 30, 40]` `for num in numbers:` `if num > 25:` `numbers.remove(num)`
A.[10, 20]
B.[10, 20, 40]
C.[10, 20, 40]
D.The code will crash.
Challenging
You have a list `scores = [85, 90, 72, 90, 68]`. You need to create a new list containing only the scores that are 90 or higher. Which code snippet does this correctly and safely?
A.high_scores = [] for s in scores: if s >= 90: high_scores.append(s)
B.for s in scores: if s < 90: scores.remove(s)
C.high_scores = scores[1:3]
D.high_scores = [s for s in scores if s < 90]
Challenging
Analyze this code block, which is based on the 'Game Inventory' example. What is the final value of `important_items`? `inventory = ['Shield', 'Potion', 'Gold Coin', 'Map']` `inventory.insert(0, 'Magic Sword')` `inventory.remove('Gold Coin')` `important_items = inventory[0:2]`
A.['Magic Sword', 'Shield', 'Potion']
B.['Magic Sword', 'Shield']
C.['Shield', 'Potion']
D.['Magic Sword', 'Potion']

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.