Skip to content

Instantly share code, notes, and snippets.

@jim-my
Forked from href/dict_namedtuple.py
Created September 22, 2021 21:04
Show Gist options
  • Save jim-my/a9b2309e1c3edff471a8629f33662fd3 to your computer and use it in GitHub Desktop.
Save jim-my/a9b2309e1c3edff471a8629f33662fd3 to your computer and use it in GitHub Desktop.
Convert any dictionary to a named tuple
from collections import namedtuple
def convert(dictionary):
return namedtuple('GenericDict', dictionary.keys())(**dictionary)
"""
>>> d = dictionary(a=1, b='b', c=[3])
>>> named = convert(d)
>>> named.a == d.a
True
>>> named.b == d.b
True
>>> named.c == d.c
True
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment