Computer Science Grade 6 20 min

List Operations

List Operations

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Add a new item to the end of a list using the .append() method. Remove a specific item from a list using the .remove() method. Access an item in a list using its index number. Change an item at a specific index in a list. Find the total number of items in a list using the len() function. Explain that Python list indices start at 0, not 1. Imagine you have a shopping list for the grocery store, but you keep remembering new things to add or deciding to remove items. How can you tell a computer to manage that list for you? 🛒 In this lesson, you will learn how to work with Python lists! We will explore how to add, remove, find, and change items in a list. This is a super important skill for making fun programs like games and apps. Real-World Applications...
2

Key Concepts & Vocabulary

TermDefinitionExample ListA special type of variable that can hold an ordered collection of items. Lists are created using square brackets [ ].my_pets = ["dog", "cat", "fish"] ElementA single item inside a list.In the list my_pets, "dog" is the first element. IndexThe position number of an element in a list. The first item always has an index of 0.In my_pets = ["dog", "cat", "fish"], the index of "dog" is 0, and the index of "fish" is 2. AppendAn operation that adds a new element to the very end of a list.my_pets.append("hamster") would add "hamster" to the end of the list. RemoveAn operation that finds and deletes the first matching element from a list.my_pets.remove("cat&qu...
3

Core Syntax & Patterns

Adding an Item (Append) list_name.append(new_item) Use this when you want to add a new item to the end of an existing list. The item goes inside the parentheses. Removing an Item list_name.remove(item_to_remove) Use this when you want to remove a specific item from a list. You must know the exact value of the item you want to remove. Accessing an Item by Index list_name[index_number] Use this to get the value of an item at a specific position. Remember, the first item is at index 0. Changing an Item by Index list_name[index_number] = new_value Use this to replace an existing item in the list with a new one. You identify the item to change by its index number.

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
Consider the following code. What will be printed? `list1 = [1, 2, 3] list2 = list1 list2.append(4) print(list1)`
A.[1, 2, 3]
B.[1, 2, 3, 4]
C.[4]
D.An error will occur
Challenging
You have a list `scores = [10, 30, 20]`. Which set of commands will change it to `[10, 20, 30, 40]`?
A.scores.sort() scores.append(40)
B.scores.append(40) scores.sort()
C.scores.remove(30) scores.insert(1, 20) scores.append(40)
D.scores.reverse() scores.append(40)
Challenging
What is the output of this code? `items = ["a", "b", "c", "a"] items.remove("a") print(items)`
A.['b', 'c']
B.['b', 'c', 'a']
C.['a', 'b', 'c']
D.['b', 'c', 'a']

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 Python Programming

Ready to find your learning gaps?

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