[Tutor] list of dicts <-> dict of lists?
David Perlman
dperlman at wisc.edu
Fri May 28 21:13:48 CEST 2010
Aha, now this is the clever solution that I didn't find "outside the
box". :)
On May 28, 2010, at 2:33 AM, Peter Otten wrote:
> I think it's simpler and therefore more appropriate to use a normal
> csv.writer here:
>
>>>> import csv
>>>> import sys
>>>> data = {'a': [1, 2, 3], 'c': [7, 8, 9], 'b': [4, 5, 6]}
>>>> writer = csv.writer(sys.stdout)
>>>> writer.writerow(data.keys())
> a,c,b
>>>> writer.writerows(zip(*data.values()))
> 1,7,4
> 2,8,5
> 3,9,6
>
> Peter
--
-dave----------------------------------------------------------------
"Pseudo-colored pictures of a person's brain lighting up are
undoubtedly more persuasive than a pattern of squiggles produced by a
polygraph. That could be a big problem if the goal is to get to the
truth." -Dr. Steven Hyman, Harvard
More information about the Tutor
mailing list