Lists and tuples: several values together
Chapter 19 of 132 · Level 1 · Beginner Python textbook · 7 real coding demonstrations + 2 worked code answers. Learn through reading, predicting and testing real Python code.
Course disclaimer · independent educational content
01 · Start here: simple explanation
Store multiple related values together so you can process them with one loop.
A list stores values in a particular order and can be changed. Place items between square brackets, separated by commas. Lists can contain duplicates.
Use indexing to get one item and slicing to get several. Similar to strings, a list begins at index 0. Lists can grow through append and change by assigning to an index.
Sorting with sorted(items) returns a new sorted list. items.sort() changes the original list. This distinction between returning a new value and changing an existing object is important.
A tuple is also ordered, but you cannot replace its elements. Parentheses are common when writing tuples. Tuples are useful for a small fixed record such as width and height.
02 · Learning objectives and technical words
By the end you can…
- Create and inspect lists
- Add, remove, and sort values
- Understand the role of immutable tuples
Words you should know
- list
- an ordered, mutable collection
- tuple
- an ordered, immutable collection
- append
- add a value to the end of a list
- mutation
- changing an existing object
03 · Point-by-point: understand the idea
Locate the input, identify the Python instruction that changes it, and compare the printed result. Make a prediction before each run so you understand what the program does—not only how to copy it.
Do not call print(items.append("new")) expecting the updated list; append changes the list and returns None. First append, then print the list.
04 · Coding examples with real outputs
Example 1: Create and read a list
Python · runnablefruits = ["apple", "pear", "plum"]
print(fruits[0])
print(fruits[-1])
apple plum
Why it works: A list has the same zero-based indexing rule as a string.
Example 2: Add an item
Python · runnabletasks = ["study", "walk"]
tasks.append("read")
print(tasks)
['study', 'walk', 'read']
Why it works: append changes the existing list and adds one value.
Example 3: Change one item
Python · runnablecolors = ["red", "blue"]
colors[1] = "green"
print(colors)
['red', 'green']
Why it works: Lists are mutable: an indexed element can be replaced.
Example 4: Take a slice
Python · runnablenumbers = [10, 20, 30, 40]
print(numbers[1:3])
[20, 30]
Why it works: The slice returns elements at indexes 1 and 2, not 3.
Example 5: Sorted copy vs original
Python · runnablescores = [8, 3, 6]
ordered = sorted(scores)
print(ordered)
print(scores)
[3, 6, 8] [8, 3, 6]
Why it works: sorted leaves scores alone and creates an ordered copy.
Example 6: Remove a known value
Python · runnableshopping = ["milk", "bread", "eggs"]
shopping.remove("bread")
print(shopping)
['milk', 'eggs']
Why it works: remove deletes the first matching value. It raises an error if the value is missing.
Example 7: Use a tuple for coordinates
Python · runnablepoint = (3, 7)
x, y = point
print("X:", x)
print("Y:", y)
X: 3 Y: 7
Why it works: Tuple unpacking assigns the two elements to descriptive names.
05 · Exercises, debugging and worked answers
Try without looking
- Create a list of three favorite foods and print the second food.
- Add "tea" to a drinks list and print the length.
- Explain why tuple values should not be assigned using an index.
Worked answers
Worked solution 1: Worked answer: second food
Python · runnablefoods = ["rice", "soup", "salad"]
print(foods[1])
soup
Why it works: The second item is at index 1.
Worked solution 2: Worked answer: append and length
Python · runnabledrinks = ["water", "juice"]
drinks.append("tea")
print(len(drinks))
3
Why it works: len reports the number of items after adding tea.
06 · Build a mini-project and review
Mini-build: A simple reading list that displays numbered book titles and marks one entry as completed.
Can you explain the output before running the code? Can you change the inputs without breaking the program? Revisit the examples until you can do both.
