Skip to content

Instantly share code, notes, and snippets.

@muhammadfredo
Forked from href/dict_namedtuple.py
Created January 15, 2018 02:07
Show Gist options
  • Select an option

  • Save muhammadfredo/e9c9046d85e573651fc65e1bf766b8ad to your computer and use it in GitHub Desktop.

Select an option

Save muhammadfredo/e9c9046d85e573651fc65e1bf766b8ad 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