Tuples as Data Records · Lecture 05 · 14 min
Named Tuples - Application - Alternative to Dictionaries
Turn dictionaries with string keys into named tuples safely with `**` unpacking, keep the dictionary conveniences via `getattr`, and convert a whole list of dicts with a shared structure.
When a dictionary is really a record
Dictionaries are often used as little records: a fixed set of string keys, created once, then only read.
data_dict = dict(key1=100, key2=200, key3=300)
data_dict['key2']If the keys never change and you don’t need to mutate the values, a named tuple fits that job well: field access reads better (data.key2), the record is immutable, and every instance shares one set of field names stored on the class instead of carrying its own hash table.
import sys
from collections import namedtuple
Data = namedtuple('Data', 'key1 key2 key3')
print(sys.getsizeof(data_dict), sys.getsizeof(Data(100, 200, 300)))(Exact sizes vary between versions, but the tuple is consistently the smaller one.)
Dictionary keys can be any hashable object: ints, tuples, and so on. Named tuple fields must be valid identifiers that don’t start with an underscore. Everything in this lesson assumes keys like 'first_name', not 42 or '_private' or 'first name' (rename=True can patch invalid names, but then the fields no longer match the keys).
From one dictionary to a named tuple
Rather than typing the field names, take them from the dictionary itself. namedtuple accepts any iterable of strings, including dict.keys():
Data = namedtuple('Data', data_dict.keys())
Data._fieldsNow for the values. It’s tempting to unpack data_dict.values() positionally:
d1 = Data(*data_dict.values())
d1That looks right, but it only works because the dictionary happened to list its values in the same order as the named tuple’s fields. Watch what happens with a dictionary whose keys were inserted in a different order:
data_dict_2 = dict(key1=100, key3=300, key2=200)
d2 = Data(*data_dict_2.values())
d2key2 received 300 and key3 received 200, silently. Dictionaries preserve insertion order, which isn’t necessarily the order of your fields.
The robust approach is to unpack the dictionary with **, which passes each item as a keyword argument. The field names are the constructor’s parameter names, so every value lands in the right place regardless of order:
d2 = Data(**data_dict_2)
d2So the pattern is:
Struct = namedtuple('Struct', d.keys())
record = Struct(**d)
A bonus of **: a key that isn’t a field (or a missing one) raises an error instead of quietly producing a wrong record.
try:
Data(**{'key1': 1, 'key2': 2, 'key4': 4})
except TypeError as ex:
print('TypeError:', ex)Choosing the field order
Because the values are matched by name, you’re free to order the fields however you like, for example alphabetically:
data_dict = dict(first_name='John', last_name='Cleese', age=42, complaint='dead parrot')
Struct = namedtuple('Struct', sorted(data_dict.keys()))
print(Struct._fields)
person = Struct(**data_dict)
print(person)
print(person.complaint)Keeping the dictionary conveniences
Two common dictionary idioms seem to have no named tuple equivalent at first.
The key is in a variable. With a dict you write data_dict[key_name]. With a named tuple, person.key_name looks for a field literally called key_name. Use the built-in getattr instead:
key_name = 'age'
print(data_dict[key_name])
try:
person.key_name
except AttributeError as ex:
print('AttributeError:', ex)
print(getattr(person, key_name))A default for missing keys. dict.get(key, default) has a direct counterpart: getattr(obj, name, default).
print(data_dict.get('age', None), data_dict.get('invalid_key', None))
print(getattr(person, 'age', None), getattr(person, 'invalid_field', None))And if you need a real dictionary again, person._asdict() gives you one.
A list of dictionaries with a common structure
Converting a single dictionary isn’t worth much: you’d create a whole class for one object. The approach pays off when you have many dictionaries that share (mostly) the same keys, such as rows parsed from JSON. One class serves them all.
data_list = [
{'key1': 1, 'key2': 2},
{'key1': 3, 'key2': 4},
{'key1': 5, 'key2': 6, 'key3': 7},
{'key2': 100},
]Not every dictionary has every key, so first collect all keys used anywhere. A set removes duplicates; a set comprehension does it in one line:
keys = {key for d in data_list for key in d.keys()}
print(sorted(keys))
# the same with set union: unpack every keys view as an argument
print(sorted(set().union(*(d.keys() for d in data_list))))A set has no meaningful order (it can even differ between runs), so sort the keys before using them as field names; otherwise the field order of your class would be unpredictable.
Then give every field a default of None, so dictionaries that lack a key still convert:
Struct = namedtuple('Struct', sorted(keys), defaults=(None,) * len(keys))
tuple_list = [Struct(**d) for d in data_list]
for t in tuple_list:
print(t)Packaged as a function:
def tuplify_dicts(dicts):
keys = sorted({key for d in dicts for key in d.keys()})
Struct = namedtuple('Struct', keys, defaults=(None,) * len(keys))
return [Struct(**d) for d in dicts]
records = tuplify_dicts(data_list)
print(records[2])
print(sum(r.key2 for r in records))Older notebooks set the defaults after creating the class with Struct.__new__.__defaults__ = (None,) * len(Struct._fields). That still works, but the defaults= argument (Python 3.7+) used above does the same in one step and keeps Struct._field_defaults correct. Older material also leaves the set of keys unsorted, which makes the field order depend on hashing; sorting it makes the result reproducible.
Mutable or not? Choosing the right tool
A named tuple is a good replacement for a dict only when you’re happy with an immutable record. Some alternatives, all in the standard library:
dict: arbitrary or changing keys, non-string keys, lookups by computed key.namedtuple/typing.NamedTuple: fixed fields, immutable, tuple behavior, very light.dataclasses.dataclass: fixed fields, mutable by default, a normal class with methods.dataclasses.make_dataclass(name, fields)builds one dynamically, likenamedtupledoes.types.SimpleNamespace: attribute access over arbitrary, mutable attributes, with no fixed schema.
from types import SimpleNamespace
from dataclasses import make_dataclass
ns = SimpleNamespace(**data_dict)
ns.age += 1 # mutable
print(ns)
Row = make_dataclass('Row', sorted(data_dict))
print(Row(**data_dict))Key takeaways
- A dictionary with a fixed set of valid string keys can become a named tuple:
Struct = namedtuple('Struct', d.keys()). - Fill it with
Struct(**d)(keyword arguments), notStruct(*d.values()), which depends on insertion order. getattr(obj, name)andgetattr(obj, name, default)replaced[name]andd.get(name, default).- For a list of dicts: collect all keys with a set, sort them, use
defaults=(None,) * n, then convert each dict. - Named tuples are immutable; use a dataclass or
SimpleNamespaceif you need to change values.