PY-101 ยท Collections
Lists
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก 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.
cart = ["tea", "milk"]cart.append("bread")cart.insert(0, "sugar")print(cart)last = cart.pop()print("Removed", last)cart.remove("milk")print(cart, len(cart))
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
['sugar', 'tea', 'milk', 'bread']
Removed bread
['sugar', 'tea'] 2 ๐ฏ Start with scores = [40, 75, 90]. Append 60, remove 40, then print the list and its average rounded to 1 decimal place.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
scores.append(60); scores.remove(40); print(scores); print(round(sum(scores) / len(scores), 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.
- The Python idea
items = [a, b, c]A list is an ordered collection of values in square brackets, such as ["tea", "milk"]. - 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() 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.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Tuples โ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. Usesorted(items)when you want a new list.
Syntax
items = [a, b, c]
items.append(x)
items.pop() Official documentation
- More on lists (docs.python.org)