Skip to Content
Course content

32: Lists: Creating and Modifying

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

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:

  1. Create a list called guest_list with three initial names: "Alice", "Bob", and "Charlie".
  2. A new guest, "Diana", RSVPs. Add her to the end of the list.
  3. "Bob" realizes he has a conflict and can't make it. Remove "Bob" from the list by name.
  4. A VIP guest, "Eve", arrives. Insert her at the very beginning of the list (index 0).
  5. The last person on the list just canceled. Use .pop() to remove them and print a message saying "Removed [Name] from the list".
  6. Finally, print the final guest_list to verify the changes.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.