Tuples and Other Immutable Collections
A list is an ordered, mutable collection of items created with square brackets and commas
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")
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
Starting the first index at 1
Python list indexes start at 0.
Fix:
Use index 0 for the first item and index 1 for the second item.Expecting append to insert an item at the beginning
append adds the item after the items already in the list.
Fix:
Remember that append adds the new item at the end.Forgetting that del uses an index
The statement selects the item in position 1, not an item by a descriptive name.
Fix:
Check the current index before deleting.Assuming indexes stay attached to items after deletion or sorting
Deleting closes the gap and sorting rearranges positions.
Fix:
Treat indexes as positions in the list's current order.Assuming list operations create an unchanged original list
Lists are mutable, so append, del, and sort modify the original list directly.
Fix:
Expect 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.
Read index 1: The item at index 1 is "ant" because the first item is at index 0.
Append: Adding "cat" produces ["zebra", "ant", "mouse", "cat"].
Delete index 0: Removing "zebra" leaves ["ant", "mouse", "cat"].
Sort: Alphabetical sorting changes the order to ["ant", "cat", "mouse"].
The requested item is "ant", and the final list is ["ant", "cat", "mouse"].
Key Takeaways
- Create a list with square brackets and commas.
- List indexes start at 0, so the first item is at index 0.
- append adds an item to the end of the original list.
- del removes an item by index, and the remaining indexes can change.
- sort arranges the original list alphabetically; these direct changes are possible because lists are mutable.
Key Takeaways
- 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.