I am trying to iterate over a dictionary in Python. The output has to resemble a certain format so it can be used as input for something else. The input looks like this:
"ok":"1",
"invalid", "0",
"unknown", "4",
...
The output should look like this:
operation,ok=1,invalid=0,unknown=4,...
I am really close to getting it with a for loop but I read that this can be done very easily with .join() in Python. Here is my code right now:
output_str = 'operation,'
for key, val in events.items():
output_str += '{0}={1},'.format(key, val)
print output_str
Can I do this using .join? I read that join will allow me to skip the comma on the last iteration. I'm just not sure how to prepend my "operation," string to it all if I do it this way.
output_str += ','.join('{0}={1}'.format(key,val) for key,val in events.items())to replace the entireforloop...