Computer Science Grade 7 20 min

Adding and Removing Items

Adding and Removing Items

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Explain that lists are dynamic and can change in size. Use the `append()` method to add an item to the end of a list. Use the `insert()` method to add an item at a specific index in a list. Use the `pop()` method to remove an item from a specific index. Use the `remove()` method to delete the first occurrence of a specific value. Predict the final state of a list after a series of add and remove operations. Write a short program that modifies a list. Ever wonder how a game keeps track of your inventory as you pick up new tools and use up potions? 🎒 Let's learn the code behind it! In many programs, we work with collections of data that need to change. This lesson will teach you the advanced skills of adding and removing items from lists, which is...
2

Key Concepts & Vocabulary

TermDefinitionExample ListAn ordered, changeable collection of items. Each item has a specific position.`my_pets = ["dog", "cat", "fish"]` is a list with three items. IndexThe position of an item in a list. The first item is always at index 0.In `my_pets = ["dog", "cat", "fish"]`, the index of "dog" is 0 and the index of "fish" is 2. DynamicA term used to describe a data structure, like a list, that can grow or shrink in size.You can start with an empty list `[]` and add 100 items to it, making it grow. AppendThe action of adding a new item to the very end of a list.If we `append("hamster")` to `my_pets`, the list becomes `["dog", "cat", "fish", "hamster"]`. In...
3

Core Syntax & Patterns

Adding to the End (Append) list_name.append(item_to_add) Use this when you want to add a new item and its position at the end is all that matters. It's the simplest way to add to a list. Adding at a Position (Insert) list_name.insert(index, item_to_add) Use this when you need to place an item at a specific position. Remember, the first argument is the index, and the second is the item. Removing by Position (Pop) list_name.pop(index) Use this when you know the position (index) of the item you want to remove. If you don't provide an index, it removes the very last item. Removing by Value (Remove) list_name.remove(item_to_remove) Use this when you know the value of the item you want to remove, but not necessarily its index. It only removes the first ma...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
What is the result of running `my_list.insert(3, 'd')` on the list `my_list = ['a', 'b', 'c']`?
A.['a', 'b', 'd', 'c']
B.['d', 'a', 'b', 'c']
C.It will cause an error because index 3 is out of bounds.
D.['a', 'b', 'c', 'd']
Challenging
A program is supposed to manage a playlist. It starts with `playlist = ['Song A', 'Song C']`. The goal is to get `['Song A', 'Song B', 'Song C']`. The code is: `playlist.append('Song B')`. Why is this code incorrect for the goal?
A.It adds 'Song B' to the beginning of the list.
B.It removes 'Song C' from the list.
C.It adds 'Song B' to the end, but it should be in the middle.
D.You cannot add items to a list after it is created.
Challenging
You want to change `list1 = [1, 2, 5, 6]` into `list2 = [2, 3, 4, 5]`. Which set of operations is the most efficient way to do this using only the methods from the lesson?
A.pop(0), pop(2), insert(1, 3), insert(2, 4)
B.remove(1), remove(6), append(3), append(4)
C.pop(0), pop(0), pop(0), pop(0), append(2), append(3), append(4), append(5)
D.insert(0, 2), insert(1, 3), insert(2, 4), insert(3, 5), pop(4), pop(5), pop(6), pop(7)

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.