← All articles
Python

Day 2: Python Lists and Tuples

The real difference between lists and tuples is mutability, not syntax — with a practical example of returning a fixed pair of values versus a growing collection.

Today covers lists and tuples. Tomorrow is sets and dictionaries.


Post 1: Conceptual Understanding

Lists and tuples look almost the same. Both hold an ordered collection of items. The real difference is whether the collection can change after you create it.

A list is mutable. You can append to it, replace an item, or remove one, and it is still the same list object afterwards.

A tuple is immutable. Once it is created, you cannot assign to its positions. Trying to do so raises a TypeError.

Here is a short example:

fruits = ["apple", "banana", "cherry"]
fruits.append("mango")
fruits[0] = "apricot"
print(fruits)

point = (10, 20)
try:
    point[0] = 99
except TypeError as error:
    print("Error:", error)

Expected output:

['apricot', 'banana', 'cherry', 'mango']
Error: 'tuple' object does not support item assignment

The list changes in place. The tuple rejects the assignment, and the error message tells you exactly what went wrong.

One detail often gets missed. A tuple’s own structure is fixed, but the objects inside it are not automatically immutable. A tuple that holds a list can still have that list modified:

t = ([1, 2], 3)
t[0].append(4)
print(t)    # ([1, 2, 4], 3)

Immutability applies to the tuple’s own items, not to everything they reference. Keep that in mind when you use tuples as dictionary keys or pass them around as if they were safe from change.

Takeaway: use a list when the collection will grow, shrink, or change. Use a tuple when the group of values should stay fixed.

#python #learnpython #programming #softwaredevelopment


Post 2: Practical Application

A function that summarizes a list is a good place to see both types at work. This one returns two values, and the caller keeps adding to the list as new measurements arrive:

def min_and_max(values):
    return (min(values), max(values))

response_times_ms = [120, 85, 240, 96]
fastest, slowest = min_and_max(response_times_ms)
print(f"Fastest: {fastest} ms, slowest: {slowest} ms")

response_times_ms.append(110)
print(response_times_ms)

Expected output:

Fastest: 85 ms, slowest: 240 ms
[120, 85, 240, 96, 110]

The function returns a tuple because the result always has exactly two values, and their positions have a clear meaning: the first is the minimum, the second is the maximum. The caller unpacks them in one line. If the caller unpacks the wrong number of values, Python raises a ValueError such as “too many values to unpack (expected 2)”.

The input is a list because the data keeps changing. append() changes the same list object, so every name that refers to it sees the new value. That can surprise you:

a = [1]
b = a
b.append(2)
print(a)    # [1, 2]

Both names point to one list. If you want an independent copy, use b = a.copy() or b = list(a).

A practical rule: use a tuple for a fixed group of values that a function returns, and a list for a collection that grows, shrinks, or is shared between parts of your code. If you find yourself returning a list just to hand back two values, consider whether a tuple says more clearly what the function promises.

Takeaway: pick the type that matches the shape of the data. A fixed shape suggests a tuple, and a changing collection suggests a list.

#python #backenddevelopment #programming #softwareengineering

Read more articles