Skip to content

Instantly share code, notes, and snippets.

@vixus0
Created April 30, 2014 10:44
Show Gist options
  • Save vixus0/5b7196a334a1fc25d6f2 to your computer and use it in GitHub Desktop.
Save vixus0/5b7196a334a1fc25d6f2 to your computer and use it in GitHub Desktop.
Dict of lists -> List of namedtuples
from collections import namedtuple
x = {}
x['species'] = ['cow', 'pig']
x['sound'] = ['moo', 'oink']
tp_animal = namedtuple('animal', 'species sound')
nanimals = 2
animals = []
for i in range(nanimals):
d = {k:v[i] for k,v in x.items()}
animals.append(tp_animal(**d))
@vixus0
Copy link
Author

vixus0 commented Apr 30, 2014

Caveats: lists have to be of same (determined) size.

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