Add Elements

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — List Methods Introduction

Lists in Python come with built-in methods that allow you to manipulate them. In this chapter, we'll explore three essential methods for adding elements to lists:

• append() - Add a single item to the end of a list • insert() - Insert an item at a specific position • extend() - Add multiple elements from another iterable

These methods are essential when you want to build and grow your lists. Let's use our Codémon examples to see how they work!

Exercise 2 — The append() Method

The append() method adds an item to the end of a list. This is one of the most commonly used list methods when you need to grow a list.

# Starting with an empty Codémon team my_team = [] print("Starting team:", my_team) # Catching Codémon one by one my_team.append("Aquachu") print("After catching Aquachu:", my_team) my_team.append("Pyrochu") print("After catching Pyrochu:", my_team)

Notice how append() adds the new item to the end of the list. The method modifies the list directly and doesn't return a new list.

Exercise 3 — Using append()

Complete the code to add "Florachu" to your Codémon team using the append() method.

Hint : The append() method adds an item to the end of a list. The syntax is list_name.append(item).

Exercise 4 — The insert() Method

The insert() method adds an item at a specified position in a list. This is useful when you need to add an item somewhere other than at the end of the list.

The syntax is list.insert(index, item) where: • index is the position where you want to insert the item • item is the value you want to add

# A team with some Codémon my_team = ["Aquachu", "Pyrochu", "Florachu"] print("Original team:", my_team) # Insert a new Codémon at the beginning (index 0) my_team.insert(0, "insert_1") print("After inserting at beginning:", my_team) # Insert a new Codémon in the middle (index 2) my_team.insert(2, "insert_2") print("After inserting in the middle:", my_team)

If the index is out of range (larger than the length of the list), the item is simply appended to the end of the list (Python doesn't raise an error).

Exercise 5 — Using insert()

Complete the code to insert "Florachu" as the second Codémon in your team (at index 1).

Hint : The insert() method adds an item at a specified position. The syntax is list.insert(index, item). List indices start at 0.

Exercise 6 — The extend() Method

The extend() method adds all the elements from another list to the end of the current list.

codemons_1 = ['Aquachu'] codemons_1.extend(['Pyrochu', 'Florachu']) print("After extend():", codemons_1) # Adds each element individually

Important: extend() is different from append()! - append() adds its argument as a single item - extend() adds each element of the iterable individually

codemons_1, codemons_2 = ['Aquachu'], ['Aquachu'] codemons_1.append([['Pyrochu', 'Florachu']]) codemons_2.extend(['Pyrochu', 'Florachu']) print("After append():", codemons_1) # Adds the list as a single item print("After extend():", codemons_2) # Adds each element individually

Exercise 7 — Understanding extend()

What will be the value of team after the following code runs?

team = ['Aquachu', 'Pyrochu'] team.extend(['Florachu'])
Options
• ['Aquachu', 'Pyrochu', 'Florachu']
• ['Aquachu', 'Pyrochu', 'F', 'l', 'o', 'r', 'a', 'c', 'h', 'u']
• ['Aquachu', 'Pyrochu', ['Florachu']]
• ['Aquachu', 'Pyrochu', ('Florachu',)]

Hint : Remember: When you use extend() with a string, each character of the string is added as a separate element to the list.

Exercise 8 — Match Adding Methods

Match each method with its correct description.

Items to match
• y.append(item)
• y.insert(0, item)
• y.extend([a, b])
• y.insert(2, item)
Definitions
• Adds a single item to the end of the list
• Adds an item at the beginning of the list
• Adds multiple elements individually to the list
• Inserts an item at index 2 (third position)

Exercise 9 — Understanding insert() Position

Given this code, what will be the final team?

team = ["Aquachu", "Pyrochu", "Florachu"] team.insert(1, "Zap") print(team)
Options
• ["Zap", "Aquachu", "Pyrochu", "Florachu"]
• ["Aquachu", "Zap", "Pyrochu", "Florachu"]
• ["Aquachu", "Pyrochu", "Zap", "Florachu"]
• ["Aquachu", "Pyrochu", "Florachu", "Zap"]

Hint : Remember: insert(1, item) places the new item at index 1 (second position), pushing existing elements to the right.

Exercise 10 — Building Your Team

Complete the code to build a Codémon team step by step: 1. Add "Pyrochu" to the end 2. Insert "Aquachu" at the beginning 3. Add both "Florachu" and "Rockachu" at once

Hint : Use append() to add one item at the end, insert(0, item) to add at the beginning, and extend() to add multiple items from a list.

Exercise 11 — Challenge - Build Your Dream Team

You're building your dream Codémon team from scratch! Start with an empty list and use the three methods you've learned to create the perfect team. Your tasks: 1. Add "Pyrochu" to your empty team 2. Insert "Aquachu" at the beginning (it will be your leader) 3. Add "Florachu" at the end 4. Add multiple Codémon at once: ["Zap", "Rockachu"] Your final team should be: ["Aquachu", "Pyrochu", "Florachu", "Zap", "Rockachu"]

Starter code
# Start with an empty team my_team = [] # Task 1: Add Pyrochu to your empty team # Task 2: Insert Aquachu at the beginning (index 0) # Task 3: Add Florachu at the end # Task 4: Add Zap and Rockachu at once # Display your final team print("My dream team:", my_team)
Back to the course