iterating dicts out of a dict of matched-length lists
I have this structure:
d = {
"a": [1, 2, 3],
"b": [4, 5, 6],
"c": [7, 8, 9],
}
The lengths of the lists are guaranteed to match. I want an iterator I can
for-loop over that will serve up a dict for each column in the lists, sort
of like a zip turned back into a dict with the original key names each
time, like this:
>>> for i in iDictOfListsToDicts(d):
... print i
{"a": 1, "b": 4, "c": 7}
{"a": 2, "b": 5, "c": 8}
{"a": 3, "b": 6, "c": 9}
I'm sure I could hack something in a generator function, but I feel like
I'm missing a tight little combo of maybe two things, perhaps from
collections and/or itertools. I'd like decent efficiency and readability
if possible.
I've been toying with things that begin like this:
map(iter, d.values())
Obviously that just gives me a list of iterators of values with no related
keys, though, and I still can't for-loop over that list and get what I
want.
No comments:
Post a Comment