Python has four main built-in collection types. Knowing when each one is right — and how to convert between them — is the single most leveraged knowledge in everyday Python.
list — ordered, mutable, the workhorse
rates = [0.07, 0.08, 0.085, 0.10, 0.125]rates.append(0.13) # add to the endrates[0] # 0.07 (zero-indexed)rates[-1] # 0.13 (last item)rates[1:3] # [0.08, 0.085] (slice)rates.sort() # sorts in placelen(rates) # 6
Lists are the default sequence type. Use them whenever order matters and you might add or remove items.
tuple — ordered, immutable
Tuples look like lists but cannot be modified after creation. Use them for fixed records (a coordinate, a (date, value) pair) and as dictionary keys (which lists cannot be).
point = (3.14, 2.71)x, y = point # tuple unpackingx, y = y, x # swap, the Pythonic way# Returning multiple valuesdef stats(xs):return min(xs), max(xs), sum(xs) / len(xs)lo, hi, mean = stats([1, 2, 3, 4, 5])
dict — key-value mapping
Dictionaries store key→value associations with O(1) lookup. The most common data structure after lists.
shares = {'equity': 0.4, 'fixed_income': 0.5, 'cash': 0.1}shares['equity'] # 0.4shares['property'] = 0.05 # add a key'cash' in shares # Truefor asset, weight in shares.items():print(f'{asset}: {weight:.0%}')
set — unordered collection of unique values
Sets are useful when you want fast membership testing or to deduplicate a list. They support mathematical operations (union, intersection, difference).
tier_1 = {'KCB', 'Equity', 'Coop', 'NCBA', 'Stanbic'}tier_2 = {'Family', 'Coop', 'Sidian', 'Equity'}tier_1 & tier_2 # {'Equity', 'Coop'} (intersection)tier_1 | tier_2 # uniontier_1 - tier_2 # {'KCB', 'NCBA', 'Stanbic'} (difference)
When to use which
List: ordered, mutable — your default. Tuple: small fixed record, dict key. Dict: key→value lookup. Set: deduplication, fast membership tests. If you find yourself iterating a list to check membership, use a set instead — O(n) becomes O(1).
Exercise
Create a dict mapping three Kenyan asset classes to portfolio weights — equities 40%, fixed income 50%, cash 10%. Then print each line as 'Asset: 40%'.