Skip to Content
Course content

34: List Methods: append, extend, insert, remove

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Lists in Python are great, but they aren't static. In the real world, data changes constantly. Whether you're managing a queue, a shopping cart, or a game inventory, you need to know how to manipulate those lists on the fly. Today, we're going to build a simple guest list manager for a small workshop I'm organizing to see these methods in action.

Starting our guest list

I'll start with a basic list of people who have already confirmed. At this stage, it's just a simple array of strings.

guests = ["Alice", "Bob", "Charlie"]
print(guests) # ['Alice', 'Bob', 'Charlie']

Growing the list one by one

As more people RSVP, I need to add them to the end of the list. For this, I'll use .append(). It's the most common way to grow a list because it just takes whatever you give it and sticks it right at the bottom.

guests.append("Diana")
print(guests) # ['Alice', 'Bob', 'Charlie', 'Diana']

Handling a group invite (and my mistake)

Now, let's say a local coding club wants to send a group of three people. I have their names in a separate list: ["Eli", "Fiona", "George"]. My first instinct—and this is a mistake I still make when I'm rushing—is to just use .append() again.

group = ["Eli", "Fiona", "George"]
guests.append(group) 
print(guests) 
# Output: ['Alice', 'Bob', 'Charlie', 'Diana', ['Eli', 'Fiona', 'George']]

See that? I didn't add three people; I added one list containing three people. Now I have a nested list, which will probably break whatever code I write later to print the names. To fix this, I need to use .extend(). While .append() adds the object as a single element, .extend() iterates over the provided list and adds each element individually.

# Fixing the mistake: first, let's reset and use extend
guests = ["Alice", "Bob", "Charlie", "Diana"] 
guests.extend(group)
print(guests) # ['Alice', 'Bob', 'Charlie', 'Diana', 'Eli', 'Fiona', 'George']

Prioritizing the VIPs

I just found out that the keynote speaker, Zoe, is coming. Since she's the guest of honor, I don't want her at the end of the list; I want her right at the top. .append() and .extend() only work at the end. To put someone in a specific spot, I'll use .insert().

The first argument is the index where I want the item to go. Since Python lists start at 0, using index 0 pushes everyone else down one spot.

guests.insert(0, "Zoe")
print(guests) # ['Zoe', 'Alice', 'Bob', 'Charlie', 'Diana', 'Eli', 'Fiona', 'George']

Dealing with cancellations

Inevitably, someone has to cancel. Bob just emailed me saying he can't make it. To get him off the list, I'll use .remove(). This method searches for the first occurrence of the value you provide and deletes it.

guests.remove("Bob")
print(guests) # ['Zoe', 'Alice', 'Charlie', 'Diana', 'Eli', 'Fiona', 'George']

One thing to keep in mind: if I tried to .remove("SomeoneWhoIsntThere"), Python would throw a ValueError. In a larger app, I'd wrap this in a check, but for our simple manager, this does exactly what we need.




📋 Practical Task

Building a Digital Shopping Cart

You are building the logic for a simple e-commerce shopping cart. Write a Python script that performs the following sequence of operations:

  • Create a list called cart containing: "Laptop", "Mouse".
  • The user decides to add a "Keyboard" to their cart using .append().
  • The user adds a "Bundle Pack" consisting of a list: ["HDMI Cable", "USB Hub", "Webcam"]. Use .extend() to ensure these are added as individual items, not as a nested list.
  • The user realizes the "Laptop" is the most important item and wants it moved to the front of the list (index 0). First, .remove() the "Laptop", then .insert() it back at the beginning.
  • The user changes their mind about the "USB Hub" and removes it from the cart using .remove().

Print the final cart list to verify the order and contents.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.