I have list where each element is a dictionary.
myList = [
{mykey1:myValue1, myKey2:myValue2},
{myKey1:myValue1b, myKey2:myValue2b}
]
I want a CSV file with just values for each entry. To get this, I need a list where each element is a string of comma separate values. So it would be of the form:
myValuesOnlyList=[ "myValue1, myValue2",
"myValue1b, myValue2b"]
To do this, I do something like:
for mydict in myList:
row = ','.join(str(e) for e in mydict.values());
myValuesOnlyList.append(row);
IS there a more pythonic way to do this?
Use the
csv.DictWriterclass, it’ll write the values directly for you: