Reputation: 61
I wish to remove all the curly brackets from my current output. My current output as shown below:
{'Chin PTE LTD': {'Carrot Cake': 22, 'Chocolate Cake': 12, 'Beer': 89}, 'COQ
SEAFOOD': {'GRILLED AUSTRALIA ANGU': 1, 'CRISPY CHICKEN WINGS': 1
}}
My current code as shown below:
for merchant, product, quantity in big_list:
d[merchant][product] += quantity
print ({ k:dict(v) for k,v in d.items() })
My desired output:
'Chin PTE LTD': 'Carrot Cake': 22, 'Chocolate Cake': 12, 'Beer': 89, 'COQ
SEAFOOD': 'GRILLED AUSTRALIA ANGU': 1, 'CRISPY CHICKEN WINGS': 1
As I am still new to python, may I ask if i wish to remove the all the curly brackets in the dictionary of dictionaries. Would my desired output be achievable? If so, how should I go about doing it? Any suggestions / ideas would be appreciated. Thank you.
Upvotes: 4
Views: 17487
Reputation: 21609
Build up the string like so
d = {'Chin PTE LTD': {'Carrot Cake': 22, 'Chocolate Cake': 12, 'Beer': 89}, 'COQ SEAFOOD': {'GRILLED AUSTRALIA ANGU': 1, 'CRISPY CHICKEN WINGS': 1}}
st = ', '.join('%r: %s' % (k, ', '.join('%r: %r' % (sk, sv) for sk, sv in v.items())) for k, v in d.items())
print(st)
This code builds the string by first iterating over the outer dict. It appends the key to the string (plus a ':' in keeping with your formatting requirements). Then it iterates over the inner dict and appends the key and value the same way. It uses the %r
format specifier which means that the elements being printed are converted using their repr
function. This gives the strings their quotes, without having to manually add them.
You can't count on the order being fixed though. So for different runs you'll get slightly different orders.
Output looks like
'Chin PTE LTD': 'Carrot Cake': 22, 'Chocolate Cake': 12, 'Beer': 89, 'COQ SEAFOOD': 'GRILLED AUSTRALIA ANGU': 1, 'CRISPY CHICKEN WINGS': 1
Upvotes: 0
Reputation: 8412
import re
result={'Chin PTE LTD': {'Carrot Cake': 22, 'Chocolate Cake': 12, 'Beer': 89}, 'COQ SEAFOOD': {'GRILLED AUSTRALIA ANGU': 1, 'CRISPY CHICKEN WINGS': 1}}
expected_output=re.sub("}|{","",str(result))
Upvotes: 0
Reputation: 733
d = {'Chin PTE LTD': {'Carrot Cake': 22, 'Chocolate Cake': 12, 'Beer': 89}, 'COQ SEAFOOD': {'GRILLED AUSTRALIA ANGU': 1, 'CRISPY CHICKEN WINGS': 1}}
', '.join(['{}: {}'.format(merchant, ', '.join(['{}: {}'.format(product, quantity) for product, quantity in products.items()])) for merchant, products in d.items()])
if you are using python2 instead of python3, replace items with iteritems
Upvotes: 0
Reputation: 14145
You can do this by converting the dictionary first to a string and then replacing all the brackets with empty strings:
d = {'Chin PTE LTD': {'Carrot Cake': 22, 'Chocolate Cake': 12, 'Beer': 89}, 'COQSEAFOOD': {'GRILLED AUSTRALIA ANGU': 1, 'CRISPY CHICKEN WINGS': 1}}
print(str(d).replace("{","").replace("}", ""))
which will print what you are looking for:
'Chin PTE LTD': 'Carrot Cake': 22, 'Chocolate Cake': 12, 'Beer': 89, 'COQSEAFOOD': 'GRILLED AUSTRALIA ANGU': 1, 'CRISPY CHICKEN WINGS': 1
Upvotes: 3