Dictionaries
Look things up by name, not position. A live quote - symbol, price, volume - is a dictionary, the most useful structure in finance code.
- ·Key-value pairs
- ·A quote as a dict
- ·Getting & setting values
- ·Looping over a dict
- ·Nested dictionaries
- ·get() & defaults
Lists and tuples find things by position: "give me the third item." But in trading you far more often want to find things by name: "what's the last price of RELIANCE?" You don't care whether it's the third or the thirtieth stock - you know its name. The structure built for exactly this is the dictionary, and it is, without exaggeration, the most useful container in all of finance code. A live quote is a dictionary. An API response is a dictionary. A row of data is a dictionary. Master this one and a huge amount of real-world code suddenly makes sense.
Looking up by name, not position
A dictionary is a set of key-value pairs, written with curly braces. Each key (a name) points to a value. You look things up, update them, and add new ones - all by key:
# A dictionary stores values you look up by NAME (a "key"), not by position.
quote = {
"symbol": "RELIANCE",
"ltp": 1313.60,
"change": 13.60,
"volume": 4521000,
}
print("Symbol:", quote["symbol"]) # look up a value by its key
print("Price :", quote["ltp"])
quote["ltp"] = 1315.20 # update an existing value
quote["high"] = 1318.40 # add a brand-new key
print("Updated:", quote)Symbol: RELIANCE
Price : 1313.6
Updated: {'symbol': 'RELIANCE', 'ltp': 1315.2, 'change': 13.6, 'volume': 4521000, 'high': 1318.4}quote["ltp"] reads "follow the ltp arrow" and lands on 1313.60. Assigning to a key either updates it (if it exists) or creates it (if it doesn't) - that's how quote["high"] = 1318.40 added a new field on the spot.
A dictionary {"key": value, ...} stores key-value pairs. Look up, update, or add with dict[key]. Keys are usually strings (names); values can be anything - numbers, text, even other dictionaries.
Looping over a dictionary, safely
To walk through a dictionary, .items() gives you each key and value together. And to fetch a key that might not exist, get() saves you from a crash:
quote = {"symbol": "RELIANCE", "ltp": 1313.60, "change": 13.60}
# .items() hands you each key and its value together - perfect for a loop.
for field, value in quote.items():
print(f"{field:>8}: {value}")
# get() safely fetches a key, returning a fallback if it's missing - no crash.
print("Bid :", quote.get("bid", "not available")) symbol: RELIANCE
ltp: 1313.6
change: 13.6
Bid : not availableAsking for a missing key with square brackets - quote["bid"] when there's no bid - raises a KeyError and stops your program. quote.get("bid", "not available") instead returns your fallback and carries on. When you're unsure a key is present (very common with live data), reach for get().
Dictionaries inside dictionaries
Values can themselves be dictionaries, and this is where it gets powerful. A whole watchlist becomes a dictionary of quotes, keyed by symbol:
# Dictionaries can hold dictionaries - here, a watchlist keyed by symbol.
watchlist = {
"RELIANCE": {"ltp": 1313.60, "change": 13.60},
"TCS": {"ltp": 2109.00, "change": -8.50},
"INFY": {"ltp": 1056.60, "change": 4.20},
}
print("TCS price:", watchlist["TCS"]["ltp"]) # dig in with two keys
print("Symbols :", list(watchlist.keys())) # all the keys as a list
# Loop through every quote in the watchlist.
for symbol, data in watchlist.items():
print(f"{symbol:9} ltp {data['ltp']:>9.2f} change {data['change']:+.2f}")TCS price: 2109.0 Symbols : ['RELIANCE', 'TCS', 'INFY'] RELIANCE ltp 1313.60 change +13.60 TCS ltp 2109.00 change -8.50 INFY ltp 1056.60 change +4.20
You dig in with two keys: watchlist["TCS"]["ltp"] means "the TCS entry, then its ltp." If this shape looks important, that's because it is - it's almost exactly the JSON format that market APIs send back. We'll meet JSON properly in Chapter 22, and you'll find you already understand it.
Dictionary look-ups are almost instant - and stay that way no matter how big the dictionary grows. Whether it holds ten quotes or ten million, fetching one by its key takes about the same tiny moment, thanks to a clever structure called a hash table. (Bonus: since Python 3.7, dictionaries also remember the exact order you added keys in.) It's why dictionaries quietly power so much fast software - including parts of Python itself.
Why dictionaries matter so much
It's worth pausing on just how often this one structure shows up:
- A live quote - symbol, price, volume, bid, ask - is a dictionary.
- A JSON response from any trading or data API is dictionaries (often nested).
- A row in a pandas DataFrame behaves like a dictionary, keyed by column name.
- Configuration, settings, lookups by name - all dictionaries.
Get comfortable here and you've got a key (pun intended) that unlocks Modules 3 and 4.
Try it yourself
- Build a dictionary for a TCS quote with keys
symbol,ltpandchange, then print just the price. - Use
.get()to ask your quote for a"sector"key it doesn't have, with the fallback"unknown". - From the nested
watchlist, print every symbol whosechangeis positive. (Hint: loop with.items()and checkdata["change"]- we'll formalise the "check" in the next chapters.)
Recap
- A dictionary
{key: value}looks values up by name, not position - perfect for quotes, records and settings. - Read/update/add with
dict[key]; assigning to a new key creates it. - Loop with
.items()(key and value together); fetch safely with.get(key, default)to avoid aKeyError. - Nested dictionaries model a watchlist of quotes - and mirror the JSON that APIs return.
- Dictionaries are fast (hash tables) and, since Python 3.7, keep their insertion order.
You've now met the four core collections: lists, tuples, dictionaries - and one more to go. When all you care about is membership and uniqueness - "which symbols are in both my watchlists?" - there's a specialised tool that's perfect, and fast. Next up: the set.