Write dictionary to csv with one line per value
Question: I am quite new to Python so please excuse me if this is a really basic
question. I have a Python dictionary such as this one:

`foo = {'bar1':['a','b','c'], 'bar2':['d','e']}`

I would like to write it to a csv file, with one line per **value** and the
key as first element of each row. The output would be (whatever the quotation
marks):

    
    
    bar1,'a'
    bar1,'b'
    bar1,'c'
    bar2,'d'
    bar2,'e'
    

I have tried this [such as suggested
here](http://stackoverflow.com/questions/8685809/python-writing-a-dictionary-
to-a-csv-file-with-one-line-for-every-key-value)

    
    
    import csv
    with open('blah.csv', 'wb') as csv_file:
        writer = csv.writer(csv_file)
        for key, value in foo.items():
           writer.writerow([key, value])
    

but this gives the following output, with one line per **key** :

    
    
    bar1,"['a', 'b', 'c']"
    bar2,"['d', 'e']"
    

Thanks for your help!


Answer: This is because `[key, value]` contains multiple "values" within `value`. Try
iterating over this like this:

    
    
    for key, values in foo.items()
        for value in values:
            writer.writerow([key, value])
    

