Lists and tuples

Watch a waitlist grow, shrink and sort in place, then watch a tuple refuse to change with a TypeError, and learn when each is the right choice.

9 min Free

Python has two built-in ways to keep values in order. A list is written with square brackets, ["Arun", "Meera"], and is mutable: you can append, insert, remove and sort, and the same list object changes. A tuple is written with round brackets, ("12658", "Chennai Mail", 21.5), and is immutable: once made, its slots cannot be reassigned.

The two look similar, and indexing and slicing work the same on both, but the official tutorial describes different jobs for them. Tuples usually hold a fixed record of mixed types, read by unpacking or by position. Lists usually hold items of one kind that you loop over and change.

Press Run to execute tickets.py. The first scenario walks through the list methods and shows the waitlist changing in place. The second scenario jumps to the last line, where the program tries to change the tuple.

Ready
# A train booking: a tuple for the train, a list for the waitlist.
train = ("12658", "Chennai Mail", 21.5)
waitlist = ["Arun", "Meera", "Sunil"]

number, name, hours = train
print(name, "runs", hours, "hours")
print(waitlist[0], waitlist[-1], waitlist[1:3])

waitlist.append("Farah")
waitlist.insert(0, "Kavya")
confirmed = waitlist.pop(0)
waitlist.remove("Sunil")
print(confirmed, waitlist, len(waitlist))

waitlist.sort()
print(waitlist)
print("Meera" in waitlist, waitlist.index("Meera"))

train[2] = 22.0
print("never printed")

Press Play to run the program line by line, or step with ← →.

00/13

Variables

Nothing defined yet.

Output

Nothing printed yet.

What to notice

  1. Positions start at 0, and -1 is the last item. waitlist[1:3] takes positions 1 and 2; the end of a slice is always excluded. A slice gives a new list, so changing it leaves the original alone.
  2. In-place methods return None. append(), insert(), remove() and sort() change the list and return nothing. confirmed = waitlist.pop(0) is the exception that returns a value: the item it removed.
  3. Two names can point at one list. backup = waitlist does not copy anything; both names see every change. Use waitlist.copy() or waitlist[:] when you want a separate list.
  4. Unpacking reads a tuple in one line. number, name, hours = train needs exactly as many names as there are items. A one-item tuple needs a trailing comma: ("Chennai",).
  5. The TypeError is a feature. A tuple cannot be changed by accident, which makes it safe to use as a dictionary key or to pass around without worrying that someone else will edit it.

Use a list

Items of one kind that come and go: a waitlist, search results, the messages in a conversation. You loop over it and change it.

Use a tuple

A fixed record whose positions mean something: (latitude, longitude), (train number, name, hours). You unpack it and never edit it.

Why this matters for AI work

JSON arrays become Python lists, so a tool that returns {"results": [...]} is returning a list inside a dict, and the code that reads it loops with for. Tuples show up when a function returns more than one value, as in return lat, lon, and as fixed keys. Knowing which methods change a list in place saves you from the classic bug results = results.sort(), which quietly replaces your data with None.

Check your understanding

1. After nums = [3, 1, 2] and result = nums.sort(), what is result?
2. Which of these is a tuple with one item?
3. waitlist is ["Arun", "Meera", "Sunil", "Farah"]. What is waitlist[1:3]?