Computer Science
Grade 7
20 min
Lesson 4: List Operations: Adding, Removing, and Accessing Items
Learn basic list operations like adding elements, removing elements, and accessing elements by index.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define a list and its key properties, like ordered items and zero-based indexing.
Use the append() method to add a new item to the end of a list.
Use the insert() method to add an item at a specific position in a list.
Retrieve a specific item from a list using its index number.
Use the remove() method to delete an item from a list based on its value.
Use the pop() method to delete an item from a list based on its index.
Ever made a to-do list and needed to add a new task or cross one off? 📝 That's exactly what we'll learn to do with code today!
In this lesson, you'll discover how to manage lists of information in your programs. You'll learn the commands to add new items, remove old ones, and find the exact piece of information you n...
2
Key Concepts & Vocabulary
TermDefinitionExample
ListAn ordered collection of items. You can store anything in a list, like numbers, words, or even other lists.my_pets = ["cat", "dog", "fish"]
Item (or Element)A single piece of data inside a list.In the list my_pets = ["cat", "dog", "fish"], the string "dog" is an item.
IndexThe position of an item in a list. The first item is always at index 0, the second at index 1, and so on.In my_pets = ["cat", "dog", "fish"], the item "cat" is at index 0.
AppendThe action of adding a new item to the very end of a list.my_pets.append("hamster") would make the list ["cat", "dog", "fish", "hamster"].
InsertThe action of ad...
3
Core Syntax & Patterns
Accessing an Item
list_name[index]
Use the list's name followed by square brackets containing the index number to get the item at that position. Remember, the first item is at index 0.
Adding Items
list_name.append(item) OR list_name.insert(index, item)
Use .append() to add an item to the end of the list. Use .insert() to add an item at a specific index, pushing other items back.
Removing Items
list_name.remove(value) OR list_name.pop(index)
Use .remove() when you know the value of the item you want to delete. Use .pop() when you know the index of the item you want to delete.
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
Your to-do list is `todo = ['laundry', 'dishes', 'homework']`. You just finished your homework (the last item) and want to remove it. You also need to add 'walk the dog' to the very beginning of the list. Which two commands, in the correct order, are the most direct way to do this?
A.todo.pop() and then todo.insert(0, 'walk the dog')
B.todo.remove('homework') and then todo.append('walk the dog')
C.todo.pop(2) and then todo.append('walk the dog')
D.todo.remove('laundry') and then todo.insert(0, 'walk the dog')
Challenging
Consider the following code: `my_list = ['a', 'b', 'c', 'd']`, `item = my_list.pop(1)`, `my_list.insert(0, item)`. What is the final state of `my_list`?
A.['a', 'c', 'd']
B.['a', 'b', 'c', 'd']
C.['b', 'a', 'c', 'd']
D.['b', 'c', 'd']
Challenging
A programmer wants to remove 'Zoe' from the list `team = ['Leo', 'Mia', 'Zoe', 'Raj']`. They write the code `team.pop('Zoe')`. Why will this code cause an error?
A.Because 'Zoe' is not the last item in the list.
B.Because `pop()` requires an integer index, not a string value.
C.Because the `pop()` method cannot be used on lists of strings.
D.Because the list must be empty before using `pop()`.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free