Computer Science
Grade 5
20 min
Using Lists in Programs
Using Lists in Programs
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Iterate through a list using a loop to check each item.
Use an item's index to read or change its value.
Dynamically add and remove items from a list within a program.
Use conditional statements (if/else) inside a loop to make decisions based on list items.
Explain how to find the total number of items in a list.
Create a new list by transforming items from an existing list.
Have you ever collected items in a video game? 🎒 How does the game keep track of everything in your backpack? It uses a list!
Today, we're going to learn advanced ways to work with lists, which are like super-powered containers for information. You'll learn how to loop through them, change items, and use them to make your programs smarter and more powerful.
Real-Worl...
2
Key Concepts & Vocabulary
TermDefinitionExample
Iteration (Looping)The process of repeating a set of instructions for every single item in a list, one by one.To greet every pet in the list `pets = ["cat", "dog", "fish"]`, you would iterate through the list and run the 'say hello' code for each pet.
IndexThe position number of an item in a list. The first item is always at index 0, the second is at index 1, and so on.In the list `scores = [95, 88, 100]`, the number 95 is at index 0, and 100 is at index 2.
LengthThe total number of items contained inside a list.The length of the list `colors = ["red", "green", "blue"]` is 3.
AppendTo add a new item to the very end of an existing list.If you have `shopping_list = ["milk", "eggs"...
3
Core Syntax & Patterns
Looping Through a List (Iteration)
for item in my_list:
Use this pattern to perform an action for every single item in a list. The variable 'item' will hold the value of the current list element in each pass of the loop.
Accessing an Item by Index
my_list[index_number]
Use this syntax to get or change the value at a specific position in the list. Remember that the first item is at index 0.
Adding and Removing Items
my_list.append(new_item)
my_list.remove(item_to_remove)
Use `.append()` to add an item to the end of a list. Use `.remove()` to find and delete the first occurrence of a specific item from the list.
4 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
You have a list `data = [10, 20, 30, 40]`. You want to swap the items at index 1 and index 3. A temporary variable `temp` is used. What is the correct sequence of commands?
A.data[1] = data[3]; data[3] = data[1]
B.temp = data[1]; data[3] = temp; data[1] = data[3]
C.temp = data[1]; data[1] = data[3]; data[3] = temp
D.temp = data[3]; data[1] = temp; data[3] = data[1]
Challenging
A simple sorting step involves comparing adjacent items and swapping them if they are in the wrong order. Given `nums = [5, 1, 4, 2]`, what does the list look like after ONE pass of this process from left to right?
A.[1, 4, 2, 5]
B.[1, 2, 4, 5]
C.[1, 5, 2, 4]
D.[5, 1, 2, 4]
Challenging
A list of 1s and 0s can represent a binary number. `binary = [1, 1, 0, 1]` represents the number 13. A simple way to check if a binary number is ODD is to check its very last digit. Which code correctly prints 'Odd' for this list?
A.if binary[0] == 1: print('Odd')
B.if binary[3] == 1: print('Odd')
C.if length of binary == 4: print('Odd')
D.if binary[1] == 1: print('Odd')
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free