Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an elegant way to convert a python dictionary into a comma separated keys string and a comma separated values string?

Tags:

I have a dictionary as follows:-

dict={'a':'1','b':'2', 'c':'3'}

To convert it into into a comma separated keys string key_string and a comma separated values string val_string I do the following:-

key_list=[]
val_list=[]

 for key,value in dict.iteritems():
     key_list.append(key)
     val_list.append(value)

 key_string = ','.join(key_list)
 val_string = ','.join(val_list)

The result is

 key_string = "a,b,c"
 val_string = "1,2,3" 

Is there a more efficient/elegant way to do this?