Beginner question: I have a dictionary where the values are lists of (a variable # of) strings. Ultimately, I would like to write each dictionary entry to a single tab-delimited line with the key as column 1 and the individual items from the value list as columns 2-n. I have used the following code to generate an output file with the key as column 1 and the value list as column 2, but I'm not sure how to proceed from there.
mydict = {'spider':['kate', 'susan'],
'fish':['kate'],
'dog':['andy'],
'cat':['andy','colby','jeff']}
f = open('outfile.txt', 'w')
writer = csv.writer(f, delimiter = '\t')
for key, value in orfdict.iteritems():
writer.writerow([orf] + [value])
The python documentation suggests that you can use zip() to create a list form key:value pairs, but when I try this at the interactive prompt:
>>> for key,value in mydict.iteritems():
... mypair = zip(key,value)
... print mypair
I get this strange output, so I obviously I'm not understanding things:
[('f', 'kate')]
[('c', 'andy'), ('a', 'colby'), ('t', 'jeff')]
[('s', 'kate'), ('p', 'susan')]
[('d', 'andy')]
Is the simplest way to do this going to be creating an empty list for each iteration over the dictionary, then appending to that list first the key, and then each of the values with an indented for loop? I feel like I must be missing something.