python - Removing the parentheses from the values of the dictionary when printing -
i have following function generate dictionary based on number of parameters introduced:
def find_posit(*param): query = {'colors':param} return query print(find_posit('red','blue','green'))
when printing function, returned output this:
{'colors':('red','blue','green')}
how obtain following output?
{'colors': 'red','blue','green'}
the short answer can't. that's way python defines __repr__()
esentation tuple objects.
however, create custom function "stringify" dictionary in representation want:
>>> def print_custom_dict(d): pairs = [] k, v in d.items(): pair = '{}: {}'.format(repr(k), ','.join(repr(val) val in v)) pairs.append(pair) return '{%s}' % ', '.join(pairs) >>> print_custom_dict({'colors':('red', 'blue', 'green')}) "{'colors': 'red','blue','green'}"
of course, there others options take such defining custom tuple
-ish object, , modifying __repr__()
of print desired output, in case using simple helper function imo simplest , cleanest method.
Comments
Post a Comment