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
32: Lists: Creating and Modifying
Think of a Python list like a physical shopping list you've scribbled on a piece of paper. You start with a few items, but as you walk through the store, you realize you forgot the milk, so you scribble it at the bottom. Then you see that the fancy organic cheese is too expensive, so you cross it out and write "cheddar" right over the top of it. Finally, you realize you actually have plenty of eggs at home, so you just rip that line off the paper entirely.
In Python, a list is exactly that: an ordered collection that lets you store multiple pieces of data in one variable, and more importantly, it lets you change that data whenever you want. Here is how that shopping list analogy maps to the code:
- The Paper: The list itself, defined by square brackets
[]. - Scribbling at the bottom: This is
.append(), adding a new item to the very end. - Writing over an item: This is indexing, where you target a specific position and replace its value.
- Ripping a line off: This is
.pop()or.remove(), taking something out of the collection.
Building your first collection
You create a list by wrapping your items in square brackets, separated by commas. You can put strings, integers, or even a mix of both in there—Python isn't picky about that.
# Let's track a queue of movies for a marathon
movie_night = ["Inception", "The Matrix", "Interstellar"]
print(movie_night)
Right now, we have a simple list. But the real power comes when we start messing with it while the program is running.
Swapping items by their position
Every item in a list has an address, which we call an index. Just remember: Python starts counting at zero. I've seen countless developers trip over this in their first week. If you want the first item, it's index 0.
# We decided "The Matrix" is too long; let's swap it for "Tenet"
movie_night[1] = "Tenet"
print(movie_night) # Output: ["Inception", "Tenet", "Interstellar"]
Notice that I didn't "delete" The Matrix first. I just told Python, "Go to slot 1 and put this new value there." The old value is gone forever.
Expanding the list on the fly
Most of the time, you won't know exactly what needs to be in your list when you first write the code. That's where .append() comes in. It's the most common way to grow a list.
# Someone suggests one more movie
movie_night.append("Arrival")
print(movie_night) # Output: ["Inception", "Tenet", "Interstellar", "Arrival"]
If you need to put something at the beginning or in the middle, you can use .insert(). It takes two arguments: where you want it to go, and what the item is.
# Let's put "Alien" right at the start
movie_night.insert(0, "Alien")
Cleaning up the clutter
Eventually, you'll need to remove things. You have two main options here. If you know the name of the item, use .remove(). If you know the position of the item, use .pop().
# We realized we've seen "Inception" too many times
movie_night.remove("Inception")
# Now, let's remove whatever is at the very end of the list
last_movie = movie_night.pop()
print(f"We dropped {last_movie} from the list.")
I personally prefer .pop() because it "pops" the value out and hands it to you. You can save it to a variable (like I did with last_movie) instead of it just vanishing into the void.
📋 Practical Task
Building a Dynamic Guest List for a Tech Meetup
You are managing the guest list for a small software meetup. Write a Python script that performs the following sequence of actions:
- Create a list called
guest_listwith three initial names: "Alice", "Bob", and "Charlie". - A new guest, "Diana", RSVPs. Add her to the end of the list.
- "Bob" realizes he has a conflict and can't make it. Remove "Bob" from the list by name.
- A VIP guest, "Eve", arrives. Insert her at the very beginning of the list (index 0).
- The last person on the list just canceled. Use
.pop()to remove them and print a message saying "Removed [Name] from the list". - Finally, print the final
guest_listto verify the changes.
There are no comments for now.