Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
34: List Methods: append, extend, insert, remove
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
cartcontaining:"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.
There are no comments for now.