So I have the following dict (the reason it's a dict and not a list is due to the output of the queryparams-parser
django package parsing POST values:
doctor_times = {
'1': {
'id': 'ABC',
},
'2': {
'id': 'DEF',
},
}
The plan is to merge these to a string such as "'ABC', 'DEF'"
for which I thought the following code would do:
doctorcodes = ', '.join("'%s'" % [str(doctor_times[k]['id']) for k in doctor_times.keys()])
However this results in:
"[, ', A, B, C, ', ,, , ', D, E, F, ', ]"
If I remove the '%s' %
component however it works fine, but I do need them wrapped individually in quotes.
The problem with your code is that, you are passing the entire list to
'%s' %
part. Instead move it only to the string part like this.and you don't need to format the data to
str
again with%s
. So, it becomes like thisThis could be further simplified like this