Lesson 25 of 60
โฎPrev Nextโฏ

PY-101 ยท Collections

Lists

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

25of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A list is an ordered collection of values in square brackets, such as ["tea", "milk"]. You read items by position starting at 0, and you can add, change and remove items at any time because lists are mutable. len() tells you how many items it holds.

    • Create and index a list
    • Add with append and insert
    • Remove with pop and remove

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Lists, running
    1. cart = ["tea", "milk"]
    2. cart.append("bread")
    3. cart.insert(0, "sugar")
    4. print(cart)
    5. last = cart.pop()
    6. print("Removed", last)
    7. cart.remove("milk")
    8. print(cart, len(cart))
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA to-do app needs to remember 3 tasks today and 30 tomorrow. One variable per task would never work; one list holds them all.
    2. The Python ideaitems = [a, b, c]A list is an ordered collection of values in square brackets, such as ["tea", "milk"].
    3. In real lifeA music app keeps your queue as a list: songs are appended, the next one is popped from the front, and you can drag one to a new position.

    Start with a question

    A to-do app needs to remember 3 tasks today and 30 tomorrow. One variable per task would never work; one list holds them all.

    Almost every program works with groups of things: users, orders, readings. Lists are Python's everyday container.

    Explain it like I'm 10

    A list is a shelf of numbered boxes. Box 0 is first. You can add boxes at the end, take boxes out, or swap what is in one.

    The technical explanation

    items[0] is the first item and items[-1] the last. items[1:3] slices positions 1 and 2.

    append(x) adds to the end, insert(i, x) adds at a position, pop() removes and returns the last item, remove(x) deletes the first matching value.

    A list can hold mixed types, but in practice lists usually hold one kind of thing.

    Syntax

    items = [a, b, c]
    items.append(x)
    items.pop()

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    A music app keeps your queue as a list: songs are appended, the next one is popped from the front, and you can drag one to a new position.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. nums = [5, 6, 7]. What is nums[-1]?
    2. 2. What does append do?
    3. 3. a = [1, 2, 3]; a.pop(). What is a?
    4. 4. What is len(["a", "b", "c"])?
    5. 5. Are lists mutable?

    Questions people ask

    What is the difference between append and extend in Python?

    append adds one item (even if it is a list, it goes in as one item). extend adds each item of another iterable one by one.

    How do I copy a list in Python?

    Use new = old.copy() or new = old[:]. Writing new = old makes two names for the same list, so changing one changes the other.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner What is the difference between append and extend in Python?

    append adds one item (even if it is a list, it goes in as one item). extend adds each item of another iterable one by one.

    beginner How do I copy a list in Python?

    Use new = old.copy() or new = old[:]. Writing new = old makes two names for the same list, so changing one changes the other.

    intermediate nums = [5, 6, 7]. What is nums[-1]?

    7. -1 is the last item.

    intermediate What does append do?

    Adds to the end. append adds one item at the end.

    Quick revision

    Remember

    • Create and index a list
    • Add with append and insert
    • Remove with pop and remove
    • list.sort() changes the list and returns None. x = items.sort() sets x to None. Use sorted(items) when you want a new list.

    Syntax

    items = [a, b, c]
    items.append(x)
    items.pop()

    Official documentation