CHAPTER 06 · BEGINNER4 min read

Lists and tuples

Organize ordered values, change a working list, and unpack fixed groups of related data.

Python 3.12+Free to readModule 2 of 13
YOUR NEXT SMALL WINS

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.

study_queue.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

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.

list_operations.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

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.

fixed_records.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

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.

unpack_records.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
LET IT CLICK

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.
TURN UNDERSTANDING INTO SOMETHING YOU CAN DO

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.

01

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.

your_turn_1.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.pyPython
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.

02

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.

your_turn_2.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.pyPython
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.

A SMALL QUESTION. A USEFUL NEXT STEP.

What stayed with you?

Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.

A LITTLE ACTIVE RECALL0 / 3 answered
01What is stored in result after values = [2, 1]; result = values.sort()?
02Which expression creates a tuple containing exactly one string?
03What happens when [1, 2] is appended to [0]?
⚡CODING CHALLENGE
🔒Required to Mark Lesson Complete

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.

EXPECTED OUTPUT
Read,Code,Review
plan.pop()
challenge.pyPython 3
Ctrl + ↵
Execution Console
Click "Run & Verify" (or press Ctrl + Enter) to test your code against the challenge.

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.

Solve the hands-on coding challenge in the knowledge section to verify your code and unlock chapter completion.