>>> example = defaultdict(list)
>>> example['key'].append(1)
>>> example
defaultdict(<class 'list'>, {'key': [1]})
I am using defaultdict for a list in my django app but a want to obtan an output witout <{}[] is it possible?
CodePudding user response:
First, you must create your data. Second, you get the keys of your data. At, last, you must loop each key.
You can use the following code which is self-explainable:
import collections
info=collections.defaultdict(list,{'List of tubes': ['2324', '98', '7654'], 'List of auto:': [147, 10048, 1009, 10050, 10, 1647, 10648, 649, 1005]})
#print(info)
keys=info.keys()
for key in keys: # loop for each key
print(f'\'{key}\': ',end='')
tmp=info[key]
for i,data in enumerate(tmp):
if isinstance(data, str): # for the string type
if i!=len(tmp)-1:
print('\'%s\', '%(data),end='')
else:
print('\'%s\''%(data),end='')
else: # for the int type
if i!=len(tmp)-1:
print('%d, '%(data),end='')
else:
print('%d'%(data),end='')
print()