Computer Science
Grade 5
20 min
Introduction to Lists
Introduction to Lists
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Combine two lists into one using concatenation.
Create a smaller, new list from a bigger list using slicing.
Define and identify a nested list (a list inside a list).
Access a specific item within a nested list using double indices.
Use a 'for' loop to iterate through a list and perform an action on each item.
Explain the difference between a list's index and its length.
Have you ever wanted to organize your game inventory into different bags, or combine two teams of players into one giant team? 🎒 Let's learn how lists can do that!
We already know how to make a basic list. Now, we'll learn the superpowers of lists, like combining them, chopping them into smaller pieces, and even putting lists inside of other lists! This will hel...
2
Key Concepts & Vocabulary
TermDefinitionExample
Nested ListA list that is an item inside of another list. It's like having a box inside of a bigger box.A list of teams, where each team is its own list of players: `teams = [['Ava', 'Leo'], ['Mia', 'Zoe']]`
List SlicingCreating a new, smaller list by copying a piece (a 'slice') from a larger list.If `colors = ['red', 'orange', 'yellow', 'green']`, a slice `colors[1:3]` would create a new list `['orange', 'yellow']`.
ConcatenationJoining two or more lists together to create one, single, longer list.If `list1 = [1, 2]` and `list2 = [3, 4]`, then `list1 + list2` results in `[1, 2, 3, 4]`.
IterationUsing a loop to go through each item in a list, one by one,...
3
Core Syntax & Patterns
Nested List Access
list_name[outer_index][inner_index]
To get an item from a list that's inside another list, you use two sets of square brackets. The first index picks the inner list, and the second index picks the item from that inner list.
List Slicing Syntax
list_name[start_index:end_index]
To create a slice, you give the index where the slice starts and the index where it ends. The slice will include the item at the start index but will stop right BEFORE the end index.
List Concatenation
new_list = list1 + list2
Use the plus sign (+) between two lists to create a new list that contains all the items from the first list followed by all the items from the second 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
Look at this code: `list_a = [1, 2, 3]; list_b = list_a; list_b.append(4)`. What will be printed if you `PRINT list_a`?
A.[1, 2, 3]
B.This will cause an error.
C.[1, 2, 3, 4]
D.[1, 2, 3]
Challenging
You have a nested list `grid = [[1, 5], [10, 2]]`. Which code snippet correctly calculates the sum of ALL the numbers in the grid (which should be 18)?
A.total = 0; FOR num IN grid: total = total + num; PRINT total
B.total = 0; FOR row IN grid: FOR num IN row: total = total + num; PRINT total
C.total = 0; FOR row IN grid: total = total + row[0]; PRINT total
D.PRINT sum(grid)
Challenging
Which code snippet correctly creates a new list called `large_numbers` that only contains numbers greater than 20 from the `all_numbers` list?
A.large_numbers = []; FOR num IN all_numbers: IF num > 20: large_numbers.append(num)
B.large_numbers = []; FOR num IN all_numbers: IF num > 20: all_numbers.remove(num)
C.large_numbers = all_numbers; FOR num IN large_numbers: IF num <= 20: large_numbers.remove(num)
D.FOR num IN all_numbers: IF num > 20: large_numbers = [num]
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free