I have a - probably - trivial question that's been bugging me for some time and I still haven't found an answer. Many of my scripts read some files, compare values from them and save these in a list or dictionary from which I then write an output file of some sort. What I always do is, I loop over the list and write the single items to my output, separated by a tab, comma, or line break. What I am always wondering is how I can prevent a separator appearing after the last item on my list has been printed.
Here is an example:
dict1 = {a: [1,2,3], b: [4,5,6], c: [7,8,9]}
for key in dict1:
outfile.write(key +": ")
for item in dict1[key]:
outfile.write(str(item) +", ")
outfile.write("\n")
The output would then look like this:
a: 1, 2, 3,
b: 4, 5, 6,
c: 7, 8, 9,
How do I avoid that last ", "?