lists of dicts.
| Type | Syntax | Ordered? | Mutable? | Duplicates? | Use it for… |
|---|---|---|---|---|---|
| list | [1, 2, 3] | yes | yes | yes | ordered sequences you'll modify |
| tuple | (1, 2, 3) | yes | no | yes | fixed records, function returns |
| dict | {"k": v} | yes | yes | keys: no | key → value lookups (O(1)) |
| set | {1, 2, 3} | no | yes | no | uniqueness & membership tests |
# block #4521 transaction hashes tx_hashes = ["0xab12", "0xcd34", "0xef56"] tx_hashes.append("0x7890") # add at end tx_hashes[0] # index → '0xab12' tx_hashes[-1] # last → '0x7890' tx_hashes[1:3] # slice len(tx_hashes) # count # loop over them for h in tx_hashes: print(h)
# (sender, receiver, amount) tx = ("alice", "bob", 12.5) # unpacking — Pythonic & clean sender, receiver, amount = tx # functions can return many values def min_max(values): return min(values), max(values) lo, hi = min_max([3, 9, 1]) tx[0] = "eve" # TypeError!
# one transaction as a dict — the shape # of every record on every blockchain tx = { "sender": "alice", "receiver": "bob", "amount": 150.0, "fee": 0.01, } tx["amount"] # 150.0 tx.get("memo", "") # safe default tx["confirmed"] = True tx.keys(); tx.values()
O(1) — the same speed whether you have 10 items or 10 million.
for k, v in tx.items(): print(k, "->", v)
# every sender who appeared in block #4521 senders = ["alice", "bob", "alice", "eve"] unique = set(senders) # {'alice', 'bob', 'eve'} # blazing-fast membership test — O(1) "bob" in unique # True # set algebra works as math block_a = {"alice", "bob"} block_b = {"bob", "eve"} block_a | block_b # union block_a & block_b # intersection → {'bob'}
len(set(senders))
across a day's blocks.
amounts = [12, 200, 8, 450, 75] big = [] for a in amounts: if a > 100: big.append(a) # 5 lines, mutable accumulator
amounts = [12, 200, 8, 450, 75] big = [a for a in amounts if a > 100] # 1 line, no accumulator # → [200, 450] # transform too — fees in paise paise = [a * 100 for a in amounts]
# list of tickers we follow tickers = ["BTC", "ETH", "DOT", "SOL"] prices = [67000, 3400, 7.2, 145] # zip + dict comprehension → ticker -> price book = {t: p for t, p in zip(tickers, prices)} # {'BTC': 67000, 'ETH': 3400, 'DOT': 7.2, 'SOL': 145} # only the large-caps (price > 100) large = {t: p for t, p in book.items() if p > 100}
# 1. import the whole module import hashlib hashlib.sha256(b"hi").hexdigest() # 2. import only what you need from hashlib import sha256 sha256(b"hi").hexdigest() # 3. give it a nickname import pandas as pd pd.DataFrame([{...}]) # your own file — same syntax from wallet import sign_tx
# write a log line with open("chain.log", "w") as f: f.write("block #4521 ok\n") # read it back, line by line with open("chain.log") as f: for line in f: print(line.rstrip()) # 'with' auto-closes the file — # even if an exception is raised.
import json block = {"number": 4521, "tx_count": 12} # write JSON with open("block.json", "w") as f: json.dump(block, f, indent=2) # read JSON with open("block.json") as f: data = json.load(f)
for loops where a comprehension fitstransactions = [ {"sender":"alice", "to":"bob", "amount":150}, {"sender":"bob", "to":"eve", "amount":50}, {"sender":"alice", "to":"eve", "amount":300}, {"sender":"carol", "to":"alice", "amount":75}, {"sender":"eve", "to":"bob", "amount":210}, # …5 more — 10 transactions total ]
sum().amount > 100. One list comprehension with an if-clause.{ sender: total_sent } across all transactions. One dict comprehension.# nested: for every unique sender, sum their amounts ledger = { s: sum(t["amount"] for t in transactions if t["sender"] == s) for s in {t["sender"] for t in transactions} }
with