Skip to content
Advertisement

I have made a dictionary using a list as the values for each key, I want to print the values without square brackets

items = ['Item 1', 'Item 2', 'Item 3']
new_items = {'bacon': items, 'bread': items, 'cheese': items}
for key, value in new_items.items():
    print('{}: {}'.format(key, *value))

Output:

bacon: Item 1
bread: Item 1
cheese: Item 1

How do I get all of the items to print? If I remove the asterisk before value it prints all 3 items, but in square brackets.

Advertisement

Answer

You can create the string to be output right in the format() method call:

items = ['Item 1', 'Item 2', 'Item 3']
new_items = {'bacon': items, 'bread': items, 'cheese': items}
for key, values in new_items.items():
    print('{}: {}'.format(key, ', '.join(values)))

Output:

bacon: Item 1, Item 2, Item 3
bread: Item 1, Item 2, Item 3
cheese: Item 1, Item 2, Item 3
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement