Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from collections import namedtuple 

2from typing import Any, Mapping 

3 

4 

5def to_namedtuple(name: str, dictionary: Mapping[str, Any]) -> Any: 

6 """Convert a dictionary to a NamedTuple. 

7 

8 Useful for generating inputs to functions that expect NamedTuples. 

9 

10 Examples: 

11 >> to_namedtuple("PandasRow", {"a": 10, "b": 20}) 

12 PandasRow(a=10, b=20) 

13 """ 

14 return namedtuple(name, dictionary.keys())(**dictionary)