The previous BitcoinVersus.tech Python lesson covered functions, parameters, and return values. This lesson moves into Python’s core collection types: lists, tuples, and sets. Choosing the right collection makes code easier to understand and helps express whether data should be ordered, changeable, or unique.
Lists: ordered and mutable
miners = ["S21", "S19", "A1566"]
miners.append("M60")
miners[1] = "S19 XP"
print(miners)
A list is useful when you need an ordered collection that can change. You can add, remove, replace, index, and slice its items.
Tuples: ordered and immutable
rack_position = ("Row-A", 12, "Upper")
row, rack, position = rack_position
print(row)
print(rack)
Tuples are sequences like lists, but the tuple itself cannot have its items reassigned after creation. Tuple unpacking is useful when a fixed group of values belongs together.
Sets: unique values
alerts = {"overheat", "offline", "overheat", "fan"}
print(alerts)
print("offline" in alerts)
A set stores unique elements and is useful for membership testing and removing duplicate values. Do not depend on a set to preserve a particular display or iteration order.
A practical data-center example
miners = ["S21-001", "S21-002", "S21-003"]
location = ("Building-1", "Row-C", "Rack-08")
faults = {"fan", "network", "fan"}
for miner in miners:
print(f"{miner}: {location}")
print(f"Unique fault types: {faults}")
Here, the list represents equipment that may change, the tuple represents a fixed location record, and the set removes duplicate fault categories automatically.
Which one should you use?
- List: ordered data you expect to modify.
- Tuple: an ordered group whose structure should remain fixed.
- Set: unique values, membership checks, and set operations such as union or intersection.
Official reference
Python documentation: Data Structures
Next step: dictionaries add key-value mappings, another essential Python structure for configuration data, telemetry, APIs, and application state.
Leave a comment