Lists and tuples
Organize ordered values, change a working list, and unpack fixed groups of related data.
By the end, you’ll be able to…
- Select and update list items with indices and slices.
- Distinguish an in-place list operation from one that returns a new list.
- Use tuples and unpacking for fixed-size groups.
01Choose an ordered collection
A list stores an ordered sequence of items. Use square brackets to build one, with commas between items. A study queue, a sequence of temperatures, or a shopping checklist all have positions that can matter. Lists support the indexing and slicing rules you learned for strings, but their items can be values of any type, including other lists.
Unlike strings, lists are mutable. You can replace the item at a particular index without building a completely new sequence. This is useful when a working plan changes. The list's length must still be considered: assigning tasks[2] requires an item already at index 2. Use append to add a new final item rather than assigning at an index equal to the current length.
02Learn what a list method returns
append adds one item, while extend adds each item from another iterable. If you append a list, that whole list becomes one nested item. If you extend with a list, its elements become additional items in the outer list. Predict the structure before printing it: the presence of an extra pair of brackets is meaningful.
Methods such as append and sort change the existing list and return None. Avoid tasks = tasks.append('Review'), which would replace the name tasks with None. By contrast, sorted(tasks) creates and returns a new sorted list. pop removes and returns an item, using the last position by default. Learn the result of each operation from its contract rather than assuming every method behaves like a string transformation.
03Tuples express a fixed arrangement
A tuple is an immutable sequence often used for a fixed group of related values, such as a location's latitude and longitude or a product's name and price. Commas create the tuple; parentheses commonly make its boundaries clear. A single-item tuple needs a trailing comma: ('Python',) is a tuple, whereas ('Python') is simply a string in parentheses.
Tuple immutability means you cannot replace its item slots. It does not guarantee every object inside the tuple is immutable; a tuple can refer to a mutable list. Use a list when the collection is expected to grow or be edited, and a tuple when a fixed arrangement communicates the meaning better. Neither choice makes the values automatically valid for your particular application.
04Unpack values into meaningful names
Sequence unpacking binds several names at once. The number of names normally must match the number of items. name, price = product states that the first item is the name and the second is the price, making subsequent code clearer than repeatedly writing product[0] and product[1]. Unpacking works for lists too; it is not restricted to tuples.
Before indexing a nested structure, describe each layer. In rows[1][0], the first subscription selects the second row, and the second selects its first item. If this notation becomes difficult to read, assign the intermediate row a descriptive name. As a self-check, draw the brackets and label the outer and inner indices; most early nested-list mistakes come from confusing those two levels.
The ideas worth keeping.
- Lists are ordered, mutable collections with zero-based indexing.
- Check whether an operation mutates, returns a value, or does both.
- Tuples and unpacking give fixed arrangements clear names.
Make it yours.
Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.
Edit a working plan
Start with ['Read', 'Code', 'Review']. Replace Code with Practice, append Rest, remove and store the last item with pop, then print the remaining plan and removed item.
Explore a worked solution
plan = ["Read", "Code", "Review"]plan[1] = "Practice"plan.append("Rest")removed = plan.pop()print(plan)print(removed)The remaining list is ['Read', 'Practice', 'Review']; pop returns Rest. Keeping its result under a separate name preserves access to the edited list.
Read a fixed record
Given record = ('Keyboard', 1200, 2), unpack it into product, unit_price, and quantity. Print Keyboard: 2400 using an f-string and a calculation.
Explore a worked solution
record = ("Keyboard", 1200, 2)product, unit_price, quantity = recordprint(f"{product}: {unit_price * quantity}")Three names match the tuple's three positions. Naming each role makes the total calculation understandable without remembering numeric indices.
What stayed with you?
Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.
Edit a working plan
Start with ['Read', 'Code', 'Review']. Replace Code with Practice, append Rest, remove and store the last item with pop, then print the remaining plan and removed item.
Read,Code,Review plan.pop()
Keep exploring.
The official references behind this chapter, when you want a second angle or the full details.
One chapter closer.
Reading is a start. Trying it yourself makes the connection.