Tuples and Other Immutable Collections
A collection holds multiple items together.
A Collection That Can Change
A collection holds multiple items together. In Python, a list is an ordered, mutable collection. Ordered means that the items have positions. Mutable means that operations can change the original list after it has been created. This makes a list useful when the contents or arrangement need to change.
The square brackets create the list, and commas separate its items. The list keeps the items in an order, so each item can later be identified by its position.
Positions Start at Zero
Python identifies a list item by its index position. The first position is index 0, not index 1. In the list ["pear", "apple", "orange"], "pear" is at index 0, "apple" is at index 1, and "orange" is at index 2. Accessing an item does not rearrange the list; it selects the item at the requested position.
first_fruit refers to "pear".
second_fruit refers to "apple".Appending at the End
The append method adds an item to a list. The new item appears after the items that were already in the list. Because lists are mutable, append modifies the original list directly.
tasks = ["read", "write"]
tasks.append("review")- 1The list begins with the items "read" and "write".
- 2append adds "review" to the list.
- 3The original list now contains "read", "write", and "review" in that order.
Deleting by Index
The del statement removes an item by its index. After the item is removed, the remaining items close the gap, so their positions can change. This is another direct modification of the original list.
tasks is now ["read", "review"].Sorting Alphabetically
The sort method arranges the items in a list alphabetically. Sorting changes the order of the existing items in the original list because lists are mutable.
fruits is now ["apple", "orange", "pear"].Lists and Immutable Collections
This topic contrasts mutable lists with immutable collections such as tuples. A list can be modified after creation: append can add an item, del can remove an item by index, and sort can rearrange its items. An immutable collection cannot be changed after creation. The supplied material gives the operational details for lists; it does not specify tuple creation syntax or tuple-specific operations.
Mistakes with List Operations
Common mistake Starting the first index at 1
What it looks likeTreating the first item in ["pear", "apple"] as index 1
Why it failsPython list indexes start at 0.
Do this insteadUse index 0 for the first item and index 1 for the second item.
Common mistake Expecting append to insert an item at the beginning
What it looks liketasks.append("first")Why it failsappend adds the item after the items already in the list.
Do this insteadRemember that append adds the new item at the end.
Common mistake Forgetting that del uses an index
What it looks likedel tasks[1]Why it failsThe statement selects the item in position 1, not an item by a descriptive name.
Do this insteadCheck the current index before deleting.
Common mistake Assuming indexes stay attached to items after deletion or sorting
What it looks likeUsing an old index after del or sort
Why it failsDeleting closes the gap and sorting rearranges positions.
Do this insteadTreat indexes as positions in the list's current order.
Common mistake Assuming list operations create an unchanged original list
What it looks likeCalling tasks.sort() and continuing as if tasks had its old order
Why it failsLists are mutable, so append, del, and sort modify the original list directly.
Do this insteadExpect the list itself to reflect the operation.
Practice the State Changes
Start with the list ["zebra", "ant", "mouse"]. Determine the item at index 1. Then append "cat", delete the item at index 0, and sort the resulting list alphabetically. State the list after each operation.
Hints
- Index 1 is the second position because indexing starts at 0.
- append adds the new item at the end.
- After del removes index 0, the remaining items move into the earlier positions.
- sort arranges the final items alphabetically.
Tracking one list through several operations
Start with ["zebra", "ant", "mouse"]. Find index 1, append "cat", delete index 0, and sort the list.
- 1Read index 1
The item at index 1 is "ant" because the first item is at index 0.
- 2Append
Adding "cat" produces ["zebra", "ant", "mouse", "cat"].
- 3Delete index 0
Removing "zebra" leaves ["ant", "mouse", "cat"].
- 4Sort
Alphabetical sorting changes the order to ["ant", "cat", "mouse"].
The requested item is "ant", and the final list is ["ant", "cat", "mouse"].
Key Takeaways
- 1Create a list with square brackets and commas.
- 2List indexes start at 0, so the first item is at index 0.
- 3append adds an item to the end of the original list.
- 4del removes an item by index, and the remaining indexes can change.
- 5sort arranges the original list alphabetically; these direct changes are possible because lists are mutable.
At a glance
- A list is an ordered, mutable collection created with square brackets and commas.
- Access items by index, beginning with index 0.
- Use append to add at the end, del to remove by index, and sort to arrange items alphabetically.
- These operations modify the original list directly.
- Tuples belong to the immutable-collection category, so they should not be treated like changeable lists.