Flat_Eric Waiting in line

List Methods

list_methods = [["part"] ['1', '2', '3']]
list_methods [0][0], [1][0]

List Methods

In Python, lists come equipped with a built-in "toolkit" known as methods. These are predefined functions designed specifically to help you manage, adjust, and organize your data efficiently.

Rather than writing complex code to change your list, you can simply call these methods to perform common tasks—like adding a person to a line, removing an item, or alphabetizing your data—instantly.

Follow the link for a complete list of list methods.

Adding and Inserting

In Python, methods are special actions we can perform on a list. Two of the most common ways to add new data are append() and insert().

Key Differences

Method Where does it go? How many arguments?
.append(item) Always the end. 1 (The item itself)
.insert(index, item) At a specific index. 2 (The spot AND the item)
  1. The .append() Method
  2. Imagine there is a line of 5 people waiting for a movie. When you arrive, you naturally go to the very back of the line. The line is now 6 people long, and you are the last item.

    In Python, append() always adds the new item to the very end of the list.

    Example:

  3. The .insert() Method
  4. Now, imagine your friend is already in line at the theater. When you show up, you don't go to the end; instead, you "cut" into the line to stand right next to them.

    In Python, insert() allows you to choose the exact index where the new item should go. Everyone else in the line simply shifts back one spot to make room for you.

    Example:

  5. The "None" Warning: A Common Mistake

    Now, imagine you tell the theater usher to put a new person in line. The usher goes and does it, but they don't come back and give you a brand-new copy of the line; they just change the one that's already there.

    In Python, methods like .append() and .insert() work "in-place." This means they modify the original list directly and do not "hand back" (return) a result. If you try to save the action to a new variable, that variable will end up empty, or None.

    Example:

    1. What happens when you print new_line?
    2. What happens when you print movie_line?

Why does this happen?

The Action: .append('Eric') successfully changes the movie_line.

The Result: Because the method's job is to change the list, not create a new one, Python gives the variable new_line a value of None (which basically means "nothing was returned").

Pro-Tip: Never write my_list = my_list.append('item'). You will accidentally delete your entire list and replace it with None! Just use the method on its own line.

Extend

The .extend() Method: The Group Arrival

Now, imagine a whole group of friends (another list) arrives at the theater at the same time. Instead of adding them one by one, you want to join the two groups together.

In Python, extend() takes an entire list and attaches it to the end of your current list. It’s like merging two separate lines into one long line.

The Movie Analogy:

You are standing in a line of 5 people. A second group of 3 people arrives. Using extend(), the second group simply joins the back of your line. The line is now 8 people long.

Why not just use .append()?

This is the "aha!" moment. If you have a line of 5 and you use append for the group of 3, Python treats the new group as one single giant person.

Example:

  1. Print the movie line
  2. Check the length of movie_line, len()
  3. Replace .extend with .append and print movie_line
  4. Check the length of movie_line again

Removing

  1. The .pop() Method: Leaving the Line

  2. To round out our movie theater analogy, we need to handle what happens when someone leaves the line. This is where .pop() comes in.

    In the theater, this usually happens for one of two reasons: either the person at the very front finally gets their ticket, or someone in the middle gets tired of waiting and leaves.

    1. Popping the End (Default)
    2. If you use .pop() without any numbers in the parentheses, Python assumes the person at the very end of the line left.

      The Movie Analogy:

      Imagine your line of 5 people. The person at the very back (Fionn) decides the movie isn't worth the wait and leaves.

      Example:

      1. Print movie line.
    3. Popping a Specific Position
    4. If you provide an index inside the parentheses, you can remove someone from a specific spot. Everyone standing behind them then steps forward to fill the gap.

      Your friend Bob (at index 1) realizes he forgot his wallet and has to leave his spot. You use .pop(1) to remove him.

      Example:

      1. Print movie line.

      The Big Difference: .pop() gives you a "Receipt"

      Unlike append, insert, and extend, .pop() is different! It actually "hands you" the item it removed.

      Think of it like this: When someone leaves the line, they hand you their ticket stub on the way out. You can "catch" that person in a variable.

      Example:

      1. Print the person with ticket.
      2. Print movie line.
  3. The .remove() Method: Identifying the Person

  4. The last way someone leaves our theater line is by name.

    While .pop() uses a position (index), .remove() looks for the value itself. This is like a security guard walking up to the line and saying, "Charlie, you need to leave the line," regardless of where Charlie is standing.

    How it works

    You don't need to know if Charlie is at index 2 or index 4. You simply tell Python to find "Charlie" and take him out.

    We have our line of 5. Charlie decides he wants to go to a different movie theater. We tell the list to remove him by name.

    Example:

    1. Print movie line.
    2. .remove('Andrew'), and print movie line.

    Important Rules for .remove()

    • First Come, First Served: If there are two people named "Alice" in the line, .remove('Alice') will only remove the first one it finds. The second Alice stays in the line.
    • The Error: If you try to remove "Zelda" and she isn't in the line, Python will throw an error (a ValueError).
    • No "Receipt": Like append and insert, .remove() works in-place. It does its job and returns None.
  5. The .clear() Method: Closing the Theater

  6. The very last method for removing items is the most drastic one: .clear().

    The Movie Analogy:

    Imagine the movie theater suddenly has a power outage. The manager comes out and announces that the screening is canceled. Everyone has to leave the line immediately.

    The line doesn't disappear (the list still exists), but it is now completely empty.

    Example:

    1. Print movie line.

Time to experiment!

Coding Exercises (VS Code) Instructions:

Exercise 1: The Late Arrivals

A line has already started, but two more people just showed up.

Exercise 2: The VIP "Cut"

Your friend 'Grace' has a VIP pass and wants to stand right at the front of the line (index 0).

Exercise 3: The Bus Load

A shuttle bus arrives with a whole group of people: ['Ivan', 'Janet', 'Kevin'].

Exercise 4: Leaving the Line

Things are getting crowded and some people are leaving. You need to handle two different ways of leaving.

Exercise 5: The Power Outage

The theater loses power and the screening is canceled. Everyone must go home.

Don't Forget to commit and Push!

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course