Skip to content

Instantly share code, notes, and snippets.

@trauber
Created April 22, 2020 18:44
Embed
What would you like to do?

Python Namedtuple to Dict to JSON

I've patched this together from several different tutorials.

from collections import namedtuple
import json

dict_list = []

Employee = namedtuple('Employee', 'FirstName, LastName, ID')
names_list = [['Michael', 'Jordan', '224567'], ['Kyle', 'Hynes', '294007'], ['Josef', 'Jones', '391107']]
employee_list = map(Employee._make, names_list)

for e in employee_list:
    dict_list.append(e._asdict())

y = json.dumps(dict_list)

print(y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment