Lesson 3: Lists Practice
45 minutes
Overview
Practice the basics of lists including creating lists and accessing, inserting, and removing elements from lists.
Purpose
This lesson is students primary opportunity to get hands on with lists in code prior to the Make activity in the following lesson. Give students as much class time as you can to work through these. For this lesson it's recommended that you place students in pairs as a support and to encourage discussion about the challenges or concepts they're seeing.
Standards
AAP-1 - To find specific solutions to generalizable problems, programmers represent and organize data in multiple ways.
AAP-1.D - For data abstraction: a. Develop data abstraction using lists to store multiple elements. b. Explain how the use of data abstraction manages complexity in program code.
- AAP-1.D.7 - The exam reference sheet provides the notation [value1, value2, value3…] to create a list with those values as the first, second, third, and so on items. For example, ● aList ← [value1, value2, value3,...] creates a new list that contains the values value1, value2, value3, and ... at indices 1, 2, 3, and ... respectively and assigns it to aList. aList ← [] creates a new empty list and assigns it to aList.
- AAP-1.D.8 - The exam reference sheet describes a list structure whose index values are 1 through the number of elements in the list, inclusive. For all list operations, if a list index is less than 1 or greater than the length of the list, an error message is produced and the program will terminate.
AAP-2 - The way statements are sequenced and combined in a program determines the computed result. Programs incorporate iteration and selection constructs to represent repetition and make decisions to handle varied input values.
AAP-2.D - Evaluate expressions that manipulate strings.
- AAP-2.D.2 - A substring is part of an existing string.
AAP-2.N - For list operations: a. Write expressions that use list indexing and list procedures. b. Evaluate expressions that use list indexing and list procedures.
- AAP-2.N.1 - The exam reference sheet provides basic operations on lists, including: ● accessing an element by index ● aList[i] accesses the element of aList at index i. The first element of aList is at index 1 and accessed using the notation aList[1]. ● assigning a value of an element of a list to a variable x aList ← [i] assigns the value of aList[i] to the variable x. ● assigning a value to an element of a list aList[i] ← x assigns the value of x to aList[i].
- AAP-2.N.2 - List procedures are implemented in accordance with the syntax rules of the programming language.
AAP-2.O - For algorithms involving elements of a list: a. Represent using iterative statements to traverse a list. b. Determine the result of an algorithm with list traversals.
- AAP-2.O.3 - The exam reference sheet providesFOR EACH item IN aList{ <block of statements>}The variable item is assigned the value of each element of aList sequentially, in order, from the first element to the last element. The code in block of statements is executed once for each assignment of item.
AP - Algorithms & Programming
- 3A-AP-14 - Use lists to simplify solutions, generalizing computational problems instead of repeated use of simple variables.
Agenda
Objectives
Students will be able to:
- Accurately use list operations including accessing, inserting, and removing elements
- Correctly set up a list in a program
- Debug programs with lists
Preparation
- Review the slide on string indexes which you'll cover at the beginning of the lesson
- Review the programming progression to understand what students will be asked to do.
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the teachers
Teaching Guide
Lesson Modifications
Attention, teachers! If you are teaching virtually or in a socially-distanced classroom, please read the full lesson plan below, then click here to access the modifications.
Warm Up (5 minutes)
Do This: Run a quick vocabulary review with students. Click through the animation to see the vocabulary definitions.
Remarks
Today we're going to have a chance to practice programming with a lot of the concepts and patterns we've explored over the last two lessons. I encourage you to go through these with a partner, but pay close attention to what each other is doing. In our next lesson you're going to have to use a lot of these on an independent project, and these activities are good practice for what you'll find there! Alright, let's get to it!
Activity (35 minutes)
Practice Time
Providing Support: Circulate around the room through the lesson encouraging students to use the strategies introduced at the beginning of the lesson. Students have a number of supports at their fingertips, so a big part of your role is helping build their independence in using those resources.
Common Errors: The following are common errors students may encounter in this lesson:
- The last index in a list is at
list.length-1
because the first index is 0. One of the most common errors with lists, therefore, is trying to access indexes that are out of bounds, most notablylist.length
, - Syntax errors are common with lists, as the bracket notation is new. Encourage students to use the blocks to help if they need.
- When using the patterns students may become overly reliant on simply copying and pasting without thinking through what the code from the patterns does. They may reference variables that don't exist in their program, for example, because it is in the example code of the pattern.
Display: Review the slides on the debugging process and remind students of important skills including the following:
- Try to zoom in on precisely where you're getting stuck.
- Talk to your partner! That's what they're there for!
- Hover over blocks to read the documentation about how they work.
- Read the resources in the Help & Tips tab
- Talk to the group next to you. If another group asks for help make sure to take some time to talk it through with them.
Display: Highlight important debugging skills specifically for lists including ways to use the Watch panel and the Debug Console with lists.
Display: Quickly introduce the fact that strings have indexes too. Students will see string.length
and string.substring()
during the lesson so this is just calling out a concept that students will need to recognize when they get to it.
Levels 1-2 Setting Up Lists: These levels involve setting up a list and printing the list to the console
- Level 1: a list of numbers
- Level 2: a list of strings
Levels 3-5 Accessing Elements in a List: Students practice accessing elements in a list using the index. These levels also practice the random list access and list scrolling pattern students saw in the Investigate lesson.
- Levels 3: Students practice printing specific elements to the console using bracket notation, as in
myList[1]
. - Level 4: Students build a "Magic 8 Ball" app that uses the Random List access pattern.
- Level 5: Students build a "Class Schedule" app that uses the List Scrolling pattern.
Extension Opportunities:
- Level 5: Ask students to add the ability to add new classes to the list in the Class Schedule app.
- Level 9: Ask students to add the ability to remove items in the Food Diary app
- Level 11: Ask students to add a button that will "Undo" adding the most recent song to the list. This is pretty tricky and requires keeping track of some new information.
Levels 6 Strings and Indexes: Students practice accessing elements in a list
- Levels 6: Students practice printing the length and first characters of strings
- Level 7: Students debug code that uses
string.substring()
to grab different parts of a date stored as a string.
Levels 8-11 List Operations: In these levels, students work with list operations: appendItem()
, removeItem()
, and insertItem()
- Level 8: Students practice using
appendItem
to increase the size of a list - Level 9: Students modify a "Food Diary" app so that the user can add new elements to a list
- Level 10: Students practice using
removeItem
andinsertItem
to modify lists - Level 11: Students modify a "Top Ten Songs" app so that the user can add new items into the middle of a list while keeping the total length of the list to 10.
Wrap Up (5 minutes)
Discussion
Discuss: What aspects of working with lists do you feel like clicked today? What do you still feel like you have trouble with?
Have students share with one another before sharing with the whole class.
Goal: Use this opportunity to address any lingering questions or misconceptions in the room. You can also use this as a source of discussion topics to kick off the following lesson. As you lead the discussion, call out the many resources students have access to help when they're getting stuck.
Remarks
Working with lists can be tricky - especially working with the index. We will get more practice tomorrow by making an app that uses lists to store information.
Assessment: Check For Understanding: AP Practice
Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.
Question: What will be displayed after this code segment is run?
Question: What will be displayed after this code segment is run?
This work is available under a Creative Commons License (CC BY-NC-SA 4.0).
If you are interested in licensing Code.org materials for commercial purposes contact us.