Concepts / Tuples and Other Immutable Collections

Tuples and Other Immutable Collections

A collection holds multiple items together.

  • Programming

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.

python

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.

Index 0pearIndex 1appleIndex 2orange
How does each index number map to a specific item in a Python list?
python
Output
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.

Code walkthrough
tasks = ["read", "write"]
tasks.append("review")
  1. 1
    The list begins with the items "read" and "write".
  2. 2
    append adds "review" to the list.
  3. 3
    The original list now contains "read", "write", and "review" in that order.
[read, write][read, write, review]append review
What changes in the list when append adds a new item, and where does the item appear?

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.

python
Output
tasks is now ["read", "review"].
Index 0: readIndex 0: readIndex 1: writeIndex 1: reviewIndex 2: reviewdeletedmoves to index 1
What happens to the remaining items and their indexes after an item is deleted?

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.

python
Output
fruits is now ["apple", "orange", "pear"].
[pear, apple,orange][apple, orange, pear]sort
How do the positions of items change when sort rearranges the list?

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.

Listcan be modifiedappend, del, sortTuplecannot be changedoriginal contentssupportsretains
What is the difference between changing a list after creation and being unable to change a tuple?

Mistakes with List Operations

  • Common mistake

    Starting the first index at 1

    What it looks like

    Treating the first item in ["pear", "apple"] as index 1

    Why it fails

    Python list indexes start at 0.

    Do this instead

    Use 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 like
    tasks.append("first")
    Why it fails

    append adds the item after the items already in the list.

    Do this instead

    Remember that append adds the new item at the end.

  • Common mistake

    Forgetting that del uses an index

    What it looks like
    del tasks[1]
    Why it fails

    The statement selects the item in position 1, not an item by a descriptive name.

    Do this instead

    Check the current index before deleting.

  • Common mistake

    Assuming indexes stay attached to items after deletion or sorting

    What it looks like

    Using an old index after del or sort

    Why it fails

    Deleting closes the gap and sorting rearranges positions.

    Do this instead

    Treat indexes as positions in the list's current order.

  • Common mistake

    Assuming list operations create an unchanged original list

    What it looks like

    Calling tasks.sort() and continuing as if tasks had its old order

    Why it fails

    Lists are mutable, so append, del, and sort modify the original list directly.

    Do this instead

    Expect the list itself to reflect the operation.

Practice the State Changes

MEDIUM

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.
Worked example

Tracking one list through several operations

Start with ["zebra", "ant", "mouse"]. Find index 1, append "cat", delete index 0, and sort the list.

  1. 1
    Read index 1

    The item at index 1 is "ant" because the first item is at index 0.

  2. 2
    Append

    Adding "cat" produces ["zebra", "ant", "mouse", "cat"].

  3. 3
    Delete index 0

    Removing "zebra" leaves ["ant", "mouse", "cat"].

  4. 4
    Sort

    Alphabetical sorting changes the order to ["ant", "cat", "mouse"].

Result

The requested item is "ant", and the final list is ["ant", "cat", "mouse"].

Key Takeaways

  1. 1
    Create a list with square brackets and commas.
  2. 2
    List indexes start at 0, so the first item is at index 0.
  3. 3
    append adds an item to the end of the original list.
  4. 4
    del removes an item by index, and the remaining indexes can change.
  5. 5
    sort 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.

Related concepts

More concepts starting with T