Computer Science
Grade 8
20 min
Lists: A Collection of Items
Introduce lists as ordered collections of items. Learn how to create and access elements in a list.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what a list is and explain its purpose in programming.
Create a list in a programming language (e.g., Python-like syntax).
Access individual items within a list using indexing.
Modify, add, and remove items from a list.
Iterate through the items of a list using a loop.
Explain the concept of mutability in the context of lists.
Ever wonder how your favorite game keeps track of all the items in your inventory or all the players on a team? 🎮 It uses something called a list!
In this lesson, you'll discover lists, a fundamental data structure in computer science. You'll learn how to create, manage, and use lists to organize multiple pieces of information efficiently, which is crucial for building complex programs.
Real-World Applications...
2
Key Concepts & Vocabulary
TermDefinitionExample
ListAn ordered collection of items, which can be of different data types, stored together under a single variable name.my_shopping_list = ["milk", "eggs", "bread"]
Element (or Item)An individual piece of data stored inside a list.In `my_shopping_list`, "milk" is an element, "eggs" is an element, and "bread" is an element.
IndexA numerical position of an element within a list, starting from 0 for the first element.In `my_shopping_list = ["milk", "eggs", "bread"]`, "milk" is at index 0, "eggs" at index 1, and "bread" at index 2.
MutabilityThe ability of a data structure to be changed (modified, added to, or removed from) after it has been created. Lis...
3
Core Syntax & Patterns
Creating a List
list_name = [item1, item2, item3, ...]
To create a list, assign a variable name to a sequence of items enclosed in square brackets `[]`, separated by commas.
Accessing List Elements by Index
list_name[index]
To retrieve a specific item from a list, use its index number inside square brackets after the list's name. Remember, indexing starts at 0.
Modifying, Adding, and Removing Elements
Modify: `list_name[index] = new_value`
Add (end): `list_name.append(new_item)`
Remove (by value): `list_name.remove(item_value)`
Remove (by index): `del list_name[index]`
Lists are mutable. You can change an element at a specific index, add new elements to the end using `append()`, or remove elements by their value or index.
5 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A student writes a loop to remove all even numbers from `numbers = [1, 2, 3, 4, 5, 6]`. The loop iterates through the list and uses `numbers.remove(num)` if `num` is even. What is the unexpected final result of the `numbers` list?
A.[1, 3, 5, 6]
B.[1, 3, 5]
C.[1, 2, 3, 5]
D.The program will crash with an error.
Challenging
You have a list `scores = [85, 92, 78, 65, 92]`. You need to first remove the lowest score (65) by its value, and then find the index of the first occurrence of the score 92. Which sequence of commands achieves this?
A.`del scores[3]`, then `scores.find(92)`
B.`scores.remove(65)`, then `scores.index(92)`
C.`scores.remove(65)`, then `scores.find(92)`
D.`del scores[65]`, then `scores.index(92)`
Challenging
A programmer writes `my_list = ("apple", "banana")` but the code fails when they try to append a new item. Based on the tutorial's 'Common Pitfalls', what is the most likely error?
A.The list items must be numbers, not text.
B.The variable name `my_list` is a reserved keyword.
C.They used parentheses `()` instead of square brackets `[]`, creating an immutable data structure (a tuple).
D.They forgot to put a comma after "banana".
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free